Skip to content

Commit

Permalink
🐛 Clean code & prep for CI enable (#7)
Browse files Browse the repository at this point in the history
* add bin dir to git
* lint and prow fix
  • Loading branch information
mjudeikis authored May 14, 2024
1 parent 84e69bf commit e49bc30
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
.vscode
vendor
hack/tools/*
bin/*
bin/gcp
13 changes: 5 additions & 8 deletions .prow.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
presubmits:
- name: pull-kcp-verify
- name: pull-gcp-verify
always_run: true
decorate: true
clone_uri: "https://github.com/kcp-dev/kcp"
clone_uri: "https://github.com/kcp-dev/generic-controlplane"
labels:
preset-goproxy: "true"
spec:
Expand All @@ -11,8 +11,6 @@ presubmits:
command:
- make
- verify-boilerplate
- verify-modules
- verify-k8s-deps
- verify-imports
resources:
requests:
Expand All @@ -22,7 +20,7 @@ presubmits:
- name: pull-gco-lint
always_run: true
decorate: true
clone_uri: "https://github.com/kcp-dev/kcp"
clone_uri: "https://github.com/kcp-dev/generic-controlplane"
labels:
preset-goproxy: "true"
spec:
Expand All @@ -49,7 +47,7 @@ presubmits:
- hack/build-image.sh
env:
- name: DRY_RUN
value: '1'
value: "1"
# docker-in-docker needs privileged mode
securityContext:
privileged: true
Expand All @@ -72,9 +70,8 @@ presubmits:
- test
env:
- name: USE_GOTESTSUM
value: '1'
value: "1"
resources:
requests:
memory: 4Gi
cpu: 2

Empty file added bin/.gitkeep
Empty file.
6 changes: 3 additions & 3 deletions server/admission/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var AllOrderedPlugins = []string{
autoprovision.PluginName, // NamespaceAutoProvision
lifecycle.PluginName, // NamespaceLifecycle
exists.PluginName, // NamespaceExists
//limitranger.PluginName, // LimitRanger
// limitranger.PluginName, // LimitRanger
serviceaccount.PluginName, // ServiceAccount
eventratelimit.PluginName, // EventRateLimit
gc.PluginName, // OwnerReferencesPermissionEnforcement
Expand All @@ -70,7 +70,7 @@ func RegisterAllAdmissionPlugins(plugins *admission.Plugins) {
autoprovision.Register(plugins)
lifecycle.Register(plugins)
exists.Register(plugins)
//limitranger.Register(plugins)
// limitranger.Register(plugins)
serviceaccount.Register(plugins)
eventratelimit.Register(plugins)
gc.Register(plugins)
Expand All @@ -89,7 +89,7 @@ func RegisterAllAdmissionPlugins(plugins *admission.Plugins) {
func DefaultOffAdmissionPlugins() sets.Set[string] {
defaultOnPlugins := sets.New(
lifecycle.PluginName, // NamespaceLifecycle
//limitranger.PluginName, // LimitRanger
// limitranger.PluginName, // LimitRanger
serviceaccount.PluginName, // ServiceAccount
defaulttolerationseconds.PluginName, // DefaultTolerationSeconds
mutatingwebhook.PluginName, // MutatingAdmissionWebhook
Expand Down
6 changes: 6 additions & 0 deletions server/cmd/options/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@ const (
gcpUserUserName = "user"
)

// AdminAuthentication holds the configuration for the admin authentication in standalone mode.
type AdminAuthentication struct {
KubeConfigPath string

// TODO: move into Secret in-cluster, maybe by using an "in-cluster" string as value
ShardAdminTokenHashFilePath string
}

// NewAdminAuthentication returns a new AdminAuthentication for the given root directory
// where the kubeconfig and the token hash file should be written.
func NewAdminAuthentication(rootDir string) *AdminAuthentication {
return &AdminAuthentication{
KubeConfigPath: filepath.Join(rootDir, "admin.kubeconfig"),
ShardAdminTokenHashFilePath: filepath.Join(rootDir, ".admin-token-store"),
}
}

// Validate validates the admin authentication configuration.
func (s *AdminAuthentication) Validate() []error {
if s == nil {
return nil
Expand All @@ -69,6 +73,7 @@ func (s *AdminAuthentication) Validate() []error {
return errs
}

// AddFlags adds the flags for the admin authentication to the given FlagSet.
func (s *AdminAuthentication) AddFlags(fs *pflag.FlagSet) {
if s == nil {
return
Expand Down Expand Up @@ -118,6 +123,7 @@ func (s *AdminAuthentication) ApplyTo(config *genericapiserver.Config) (volatile
return volatileGcpAdminToken, volatileUserToken, nil
}

// WriteKubeConfig writes the kubeconfig to the configured path.
func (s *AdminAuthentication) WriteKubeConfig(config genericapiserver.CompletedConfig, gcpAdminToken, userToken string) error {
externalCACert, _ := config.SecureServing.Cert.CurrentCertKeyContent()
externalKubeConfigHost := fmt.Sprintf("https://%s", config.ExternalAddress)
Expand Down
4 changes: 4 additions & 0 deletions server/cmd/options/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
generatedopenapi "k8s.io/kubernetes/pkg/generated/openapi"
)

// Config holds the configuration for the generic controlplane server.
type Config struct {
Options CompletedOptions

Expand All @@ -41,6 +42,7 @@ type Config struct {
ExtraConfig
}

// ExtraConfig holds the extra configuration for the generic controlplane server.
type ExtraConfig struct {
// authentication
GcpAdminToken, UserToken string
Expand All @@ -58,11 +60,13 @@ type completedConfig struct {
ExtraConfig
}

// CompletedConfig holds the completed configuration for the generic controlplane server.
type CompletedConfig struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*completedConfig
}

// Complete fills in any fields not set that are required to have valid data.
func (c *Config) Complete() (CompletedConfig, error) {
return CompletedConfig{&completedConfig{
Options: c.Options,
Expand Down
7 changes: 6 additions & 1 deletion server/cmd/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
gcpadmission "github.com/kcp-dev/generic-controlplane/server/admission"
)

// Options holds the configuration for the generic controlplane server.
type Options struct {
GenericControlPlane controlplaneapiserveroptions.Options
EmbeddedEtcd etcdoptions.Options
Expand All @@ -45,6 +46,7 @@ type Options struct {
Extra ExtraOptions
}

// ExtraOptions holds the extra configuration for the generic controlplane server.
type ExtraOptions struct {
RootDir string
}
Expand All @@ -58,6 +60,7 @@ type completedOptions struct {
Extra ExtraOptions
}

// CompletedOptions holds the completed configuration for the generic controlplane server.
type CompletedOptions struct {
*completedOptions
}
Expand Down Expand Up @@ -99,6 +102,7 @@ func NewOptions(rootDir string) *Options {
return o
}

// AddFlags adds flags for a specific APIServer to the specified FlagSet.
func (o *Options) AddFlags(fss *cliflag.NamedFlagSets) {
o.GenericControlPlane.AddFlags(fss)

Expand All @@ -112,6 +116,7 @@ func (o *Options) AddFlags(fss *cliflag.NamedFlagSets) {
// Placeholders for future flags.
}

// Complete fills in any fields not set that are required to have valid data.
func (o *Options) Complete() (*CompletedOptions, error) {
if servers := o.GenericControlPlane.Etcd.StorageConfig.Transport.ServerList; len(servers) == 1 && servers[0] == "embedded" {
klog.Background().Info("enabling embedded etcd server")
Expand Down Expand Up @@ -149,7 +154,6 @@ func (o *Options) Complete() (*CompletedOptions, error) {
}

// override set of admission plugins
//spew.Dump(o.GenericControlPlane.Admission.GenericAdmission.Plugins)
gcpadmission.RegisterAllAdmissionPlugins(o.GenericControlPlane.Admission.GenericAdmission.Plugins)
o.GenericControlPlane.Admission.GenericAdmission.DisablePlugins = sets.List[string](gcpadmission.DefaultOffAdmissionPlugins())
o.GenericControlPlane.Admission.GenericAdmission.RecommendedPluginOrder = gcpadmission.AllOrderedPlugins
Expand Down Expand Up @@ -197,6 +201,7 @@ func (o *Options) Complete() (*CompletedOptions, error) {
}, nil
}

// Validate validates the generic controlplane server options.
func (o *CompletedOptions) Validate() []error {
var errs []error

Expand Down
10 changes: 5 additions & 5 deletions server/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
"github.com/spf13/cobra"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
kerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
_ "k8s.io/apiserver/pkg/admission"
_ "k8s.io/apiserver/pkg/admission" // for admission plugins
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
genericapiserver "k8s.io/apiserver/pkg/server"
utilfeature "k8s.io/apiserver/pkg/util/feature"
Expand All @@ -41,14 +41,14 @@ import (
"k8s.io/component-base/cli/globalflag"
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
_ "k8s.io/component-base/metrics/prometheus/workqueue"
_ "k8s.io/component-base/metrics/prometheus/workqueue" // for workqueue metrics
"k8s.io/component-base/term"
"k8s.io/component-base/version"
"k8s.io/component-base/version/verflag"
"k8s.io/klog/v2"
aggregatorapiserver "k8s.io/kube-aggregator/pkg/apiserver"
controlplaneapiserver "k8s.io/kubernetes/pkg/controlplane/apiserver"
_ "k8s.io/kubernetes/pkg/features"
_ "k8s.io/kubernetes/pkg/features" // add the kubernetes feature gates

options "github.com/kcp-dev/generic-controlplane/server/cmd/options"
// add the kubernetes feature gates
Expand Down Expand Up @@ -108,7 +108,7 @@ APIs.`,
}

if errs := completedOptions.Validate(); len(errs) != 0 {
return utilerrors.NewAggregate(errs)
return kerrors.NewAggregate(errs)
}

// add feature enablement metrics
Expand Down

0 comments on commit e49bc30

Please sign in to comment.