diff --git a/cmd/controller/main.go b/cmd/controller/main.go index bcd8531..183d820 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -55,6 +55,7 @@ func init() { // +kubebuilder:scaffold:scheme } +//nolint:funlen // TODO: refactor to reduce function length func main() { var metricsAddr string var enableLeaderElection bool diff --git a/cmd/storage/server/start.go b/cmd/storage/server/start.go index 7a5c1e8..9b36b80 100644 --- a/cmd/storage/server/start.go +++ b/cmd/storage/server/start.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "io" + "math" "net" "github.com/jmoiron/sqlx" @@ -28,10 +29,8 @@ import ( utilerrors "k8s.io/apimachinery/pkg/util/errors" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/version" - "k8s.io/apiserver/pkg/admission" "k8s.io/apiserver/pkg/endpoints/openapi" genericapiserver "k8s.io/apiserver/pkg/server" - "k8s.io/apiserver/pkg/server/options" genericoptions "k8s.io/apiserver/pkg/server/options" utilfeature "k8s.io/apiserver/pkg/util/feature" utilversion "k8s.io/apiserver/pkg/util/version" @@ -40,9 +39,7 @@ import ( netutils "k8s.io/utils/net" "github.com/rancher/sbombastic/api/storage/v1alpha1" - "github.com/rancher/sbombastic/internal/admission/wardleinitializer" "github.com/rancher/sbombastic/internal/apiserver" - clientset "github.com/rancher/sbombastic/pkg/generated/clientset/versioned" informers "github.com/rancher/sbombastic/pkg/generated/informers/externalversions" sampleopenapi "github.com/rancher/sbombastic/pkg/generated/openapi" ) @@ -66,7 +63,12 @@ func WardleVersionToKubeVersion(ver *version.Version) *version.Version { } kubeVer := utilversion.DefaultKubeEffectiveVersion().BinaryVersion() // "1.2" maps to kubeVer - offset := int(ver.Minor()) - 2 + minor := ver.Minor() + if minor > math.MaxInt32 { + panic("minor version is too large") + } + + offset := int(minor) - 2 mappedVer := kubeVer.OffsetMinor(offset) if mappedVer.GreaterThan(kubeVer) { return kubeVer @@ -145,16 +147,6 @@ func NewCommandStartWardleServer(ctx context.Context, defaults *WardleServerOpti apiserver.WardleComponentName, utilversion.NewEffectiveVersion(defaultWardleVersion), featuregate.NewVersionedFeatureGate(version.MustParse(defaultWardleVersion))) - // // Add versioned feature specifications for the "BanFlunder" feature. - // // These specifications, together with the effective version, determine if the feature is enabled. - // utilruntime.Must(wardleFeatureGate.AddVersioned(map[featuregate.Feature]featuregate.VersionedSpecs{ - // "BanFlunder": { - // {Version: version.MustParse("1.2"), Default: true, PreRelease: featuregate.GA, LockToDefault: true}, - // {Version: version.MustParse("1.1"), Default: true, PreRelease: featuregate.Beta}, - // {Version: version.MustParse("1.0"), Default: false, PreRelease: featuregate.Alpha}, - // }, - // })) - // Register the default kube component if not already present in the global registry. _, _ = utilversion.DefaultComponentGlobalsRegistry.ComponentGlobalsOrRegister(utilversion.DefaultKubeComponent, utilversion.NewEffectiveVersion(baseversion.DefaultKubeBinaryVersion), utilfeature.DefaultMutableFeatureGate) @@ -169,7 +161,7 @@ func NewCommandStartWardleServer(ctx context.Context, defaults *WardleServerOpti } // Validate validates WardleServerOptions -func (o WardleServerOptions) Validate(args []string) error { +func (o WardleServerOptions) Validate(_ []string) error { errors := []error{} errors = append(errors, o.RecommendedOptions.Validate()...) errors = append(errors, utilversion.DefaultComponentGlobalsRegistry.Validate()...) @@ -185,17 +177,7 @@ func (o *WardleServerOptions) Complete() error { func (o *WardleServerOptions) Config() (*apiserver.Config, error) { // TODO have a "real" external address if err := o.RecommendedOptions.SecureServing.MaybeDefaultWithSelfSignedCerts("localhost", o.AlternateDNS, []net.IP{netutils.ParseIPSloppy("127.0.0.1")}); err != nil { - return nil, fmt.Errorf("error creating self-signed certificates: %v", err) - } - - o.RecommendedOptions.ExtraAdmissionInitializers = func(c *genericapiserver.RecommendedConfig) ([]admission.PluginInitializer, error) { - client, err := clientset.NewForConfig(c.LoopbackClientConfig) - if err != nil { - return nil, err - } - informerFactory := informers.NewSharedInformerFactory(client, c.LoopbackClientConfig.Timeout) - o.SharedInformerFactory = informerFactory - return []admission.PluginInitializer{wardleinitializer.New(informerFactory)}, nil + return nil, fmt.Errorf("error creating self-signed certificates: %w", err) } serverConfig := genericapiserver.NewRecommendedConfig(apiserver.Codecs) @@ -212,12 +194,12 @@ func (o *WardleServerOptions) Config() (*apiserver.Config, error) { serverConfig.EffectiveVersion = utilversion.DefaultComponentGlobalsRegistry.EffectiveVersionFor(apiserver.WardleComponentName) // As we don't have a real etcd, we need to set a dummy storage factory - serverConfig.RESTOptionsGetter = &options.StorageFactoryRestOptionsFactory{ - StorageFactory: &options.SimpleStorageFactory{}, + serverConfig.RESTOptionsGetter = &genericoptions.StorageFactoryRestOptionsFactory{ + StorageFactory: &genericoptions.SimpleStorageFactory{}, } if err := o.RecommendedOptions.ApplyTo(serverConfig); err != nil { - return nil, err + return nil, fmt.Errorf("error applying options to server config: %w", err) } config := &apiserver.Config{ @@ -236,14 +218,12 @@ func (o WardleServerOptions) RunWardleServer(ctx context.Context) error { server, err := config.Complete().New(o.DB) if err != nil { - return err + return fmt.Errorf("error creating server: %w", err) } - server.GenericAPIServer.AddPostStartHookOrDie("start-sample-server-informers", func(context genericapiserver.PostStartHookContext) error { - config.GenericConfig.SharedInformerFactory.Start(context.Done()) - o.SharedInformerFactory.Start(context.Done()) - return nil - }) + if err := server.GenericAPIServer.PrepareRun().RunWithContext(ctx); err != nil { + return fmt.Errorf("error while running server: %w", err) + } - return server.GenericAPIServer.PrepareRun().RunWithContext(ctx) + return nil } diff --git a/cmd/worker/main.go b/cmd/worker/main.go index a585818..bc3fe25 100644 --- a/cmd/worker/main.go +++ b/cmd/worker/main.go @@ -2,7 +2,6 @@ package main import ( "errors" - "fmt" "log" "time" @@ -26,6 +25,7 @@ func main() { log.Fatal(err) } + // TODO: placeholder, implement subscriber logic for { msgs, err := sub.Fetch(1, nats.MaxWait(5*time.Second)) if err != nil { @@ -36,11 +36,7 @@ func main() { } for _, msg := range msgs { - fmt.Printf("Received a message: %s\n", string(msg.Data)) - meta, _ := msg.Metadata() - fmt.Printf("Stream Sequence : %v\n", meta.Sequence.Stream) - fmt.Printf("Consumer Sequence: %v\n", meta.Sequence.Consumer) - msg.Ack() + _ = msg.Ack() } } } diff --git a/internal/apiserver/apiserver.go b/internal/apiserver/apiserver.go index a077972..9d707fb 100644 --- a/internal/apiserver/apiserver.go +++ b/internal/apiserver/apiserver.go @@ -17,6 +17,8 @@ limitations under the License. package apiserver import ( + "fmt" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -98,7 +100,7 @@ func (cfg *Config) Complete() CompletedConfig { func (c completedConfig) New(db *sqlx.DB) (*WardleServer, error) { genericServer, err := c.GenericConfig.New("sample-apiserver", genericapiserver.NewEmptyDelegate()) if err != nil { - return nil, err + return nil, fmt.Errorf("error creating generic server: %w", err) } s := &WardleServer{ @@ -111,14 +113,14 @@ func (c completedConfig) New(db *sqlx.DB) (*WardleServer, error) { sbomStore, err := storage.NewSBOMStore(Scheme, c.GenericConfig.RESTOptionsGetter, db) if err != nil { - return nil, err + return nil, fmt.Errorf("error creating SBOM store: %w", err) } v1alpha1storage["sboms"] = sbomStore apiGroupInfo.VersionedResourcesStorageMap["v1alpha1"] = v1alpha1storage if err := s.GenericAPIServer.InstallAPIGroup(&apiGroupInfo); err != nil { - return nil, err + return nil, fmt.Errorf("error installing API group: %w", err) } return s, nil