diff --git a/catalogd/internal/k8sutil/k8sutil.go b/catalogd/internal/k8sutil/k8sutil.go deleted file mode 100644 index dfea1d0d6..000000000 --- a/catalogd/internal/k8sutil/k8sutil.go +++ /dev/null @@ -1,17 +0,0 @@ -package k8sutil - -import ( - "regexp" - - "k8s.io/apimachinery/pkg/util/validation" -) - -var invalidNameChars = regexp.MustCompile(`[^\.\-a-zA-Z0-9]`) - -// MetadataName replaces all invalid DNS characters with a dash. If the result -// is not a valid DNS subdomain, returns `result, false`. Otherwise, returns the -// `result, true`. -func MetadataName(name string) (string, bool) { - result := invalidNameChars.ReplaceAllString(name, "-") - return result, validation.IsDNS1123Subdomain(result) == nil -} diff --git a/catalogd/internal/k8sutil/k8sutil_test.go b/catalogd/internal/k8sutil/k8sutil_test.go deleted file mode 100644 index d1b142680..000000000 --- a/catalogd/internal/k8sutil/k8sutil_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package k8sutil - -import ( - "fmt" - "strings" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestMetadataName(t *testing.T) { - type testCase struct { - name string - in string - expectedResult string - expectedValid bool - } - for _, tc := range []testCase{ - { - name: "empty", - in: "", - expectedResult: "", - expectedValid: false, - }, - { - name: "invalid", - in: "foo-bar.123!", - expectedResult: "foo-bar.123-", - expectedValid: false, - }, - { - name: "too long", - in: fmt.Sprintf("foo-bar_%s", strings.Repeat("1234567890", 50)), - expectedResult: fmt.Sprintf("foo-bar-%s", strings.Repeat("1234567890", 50)), - expectedValid: false, - }, - { - name: "valid", - in: "foo-bar.123", - expectedResult: "foo-bar.123", - expectedValid: true, - }, - { - name: "valid with underscore", - in: "foo-bar_123", - expectedResult: "foo-bar-123", - expectedValid: true, - }, - { - name: "valid with colon", - in: "foo-bar:123", - expectedResult: "foo-bar-123", - expectedValid: true, - }, - } { - t.Run(tc.name, func(t *testing.T) { - actualResult, actualValid := MetadataName(tc.in) - assert.Equal(t, tc.expectedResult, actualResult) - assert.Equal(t, tc.expectedValid, actualValid) - }) - } -} diff --git a/catalogd/internal/serverutil/serverutil.go b/catalogd/internal/serverutil/serverutil.go index b91225335..614da2b8b 100644 --- a/catalogd/internal/serverutil/serverutil.go +++ b/catalogd/internal/serverutil/serverutil.go @@ -9,10 +9,10 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/certwatcher" + "sigs.k8s.io/controller-runtime/pkg/manager" catalogdmetrics "github.com/operator-framework/operator-controller/catalogd/internal/metrics" "github.com/operator-framework/operator-controller/catalogd/internal/storage" - "github.com/operator-framework/operator-controller/catalogd/internal/third_party/server" ) type CatalogServerConfig struct { @@ -40,8 +40,9 @@ func AddCatalogServerToManager(mgr ctrl.Manager, cfg CatalogServerConfig, tlsFil shutdownTimeout := 30 * time.Second - catalogServer := server.Server{ - Kind: "catalogs", + catalogServer := manager.Server{ + Name: "catalogs", + OnlyServeWhenLeader: true, Server: &http.Server{ Addr: cfg.CatalogAddr, Handler: catalogdmetrics.AddMetricsToHandler(cfg.LocalStorage.StorageServerHandler()), diff --git a/catalogd/internal/third_party/server/server.go b/catalogd/internal/third_party/server/server.go deleted file mode 100644 index cfdec7b3b..000000000 --- a/catalogd/internal/third_party/server/server.go +++ /dev/null @@ -1,123 +0,0 @@ -/* -Copyright 2022 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// this is copied from https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/77b08a845e451b695cfa25b79ebe277d85064345/pkg/manager/server.go -// we will remove this once we update to a version of controller-runitme that has this included -// https://github.com/kubernetes-sigs/controller-runtime/pull/2473 - -package server - -import ( - "context" - "errors" - "net" - "net/http" - "time" - - "github.com/go-logr/logr" - - crlog "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/manager" -) - -var ( - _ manager.Runnable = (*Server)(nil) - _ manager.LeaderElectionRunnable = (*Server)(nil) -) - -// Server is a general purpose HTTP(S) server Runnable for a manager. -// It is used to serve some internal handlers for health probes and profiling, -// but it can also be used to run custom servers. -type Server struct { - // Kind is an optional string that describes the purpose of the server. It is used in logs to distinguish - // among multiple servers. - Kind string - - // Log is the logger used by the server. If not set, a logger will be derived from the context passed to Start. - Log logr.Logger - - // Server is the HTTP server to run. It is required. - Server *http.Server - - // Listener is an optional listener to use. If not set, the server start a listener using the server.Addr. - // Using a listener is useful when the port reservation needs to happen in advance of this runnable starting. - Listener net.Listener - - // OnlyServeWhenLeader is an optional bool that indicates that the server should only be started when the manager is the leader. - OnlyServeWhenLeader bool - - // ShutdownTimeout is an optional duration that indicates how long to wait for the server to shutdown gracefully. If not set, - // the server will wait indefinitely for all connections to close. - ShutdownTimeout *time.Duration -} - -// Start starts the server. It will block until the server is stopped or an error occurs. -func (s *Server) Start(ctx context.Context) error { - log := s.Log - if log.GetSink() == nil { - log = crlog.FromContext(ctx) - } - if s.Kind != "" { - log = log.WithValues("kind", s.Kind) - } - log = log.WithValues("addr", s.addr()) - - serverShutdown := make(chan struct{}) - go func() { - <-ctx.Done() - log.Info("shutting down server") - - shutdownCtx := context.Background() - if s.ShutdownTimeout != nil { - var shutdownCancel context.CancelFunc - shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), *s.ShutdownTimeout) - defer shutdownCancel() - } - - if err := s.Server.Shutdown(shutdownCtx); err != nil { - log.Error(err, "error shutting down server") - } - close(serverShutdown) - }() - - log.Info("starting server") - if err := s.serve(); err != nil && !errors.Is(err, http.ErrServerClosed) { - return err - } - - <-serverShutdown - return nil -} - -// NeedLeaderElection returns true if the server should only be started when the manager is the leader. -func (s *Server) NeedLeaderElection() bool { - return s.OnlyServeWhenLeader -} - -func (s *Server) addr() string { - if s.Listener != nil { - return s.Listener.Addr().String() - } - return s.Server.Addr -} - -func (s *Server) serve() error { - if s.Listener != nil { - return s.Server.Serve(s.Listener) - } - - return s.Server.ListenAndServe() -}