-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add certwatcher for TLS cert and key from controller-runtime
- Add error for missing either tls-key or tls-cert arguments. - Move server creation and configuration to serverutil Signed-off-by: Tayler Geiger <[email protected]>
- Loading branch information
Showing
2 changed files
with
95 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package serverutil | ||
|
||
import ( | ||
"crypto/tls" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/certwatcher" | ||
|
||
"github.com/operator-framework/catalogd/internal/third_party/server" | ||
catalogdmetrics "github.com/operator-framework/catalogd/pkg/metrics" | ||
"github.com/operator-framework/catalogd/pkg/storage" | ||
) | ||
|
||
type CatalogServerConfig struct { | ||
ExternalAddr string | ||
CatalogAddr string | ||
CertFile string | ||
KeyFile string | ||
LocalStorage storage.Instance | ||
} | ||
|
||
func AddCatalogServerToManager(mgr ctrl.Manager, cfg CatalogServerConfig) error { | ||
listener, err := net.Listen("tcp", cfg.CatalogAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if cfg.CertFile != "" && cfg.KeyFile != "" { | ||
tlsFileWatcher, err := certwatcher.New(cfg.CertFile, cfg.KeyFile) | ||
if err != nil { | ||
return err | ||
} | ||
config := &tls.Config{ | ||
GetCertificate: tlsFileWatcher.GetCertificate, | ||
MinVersion: tls.VersionTLS12, | ||
} | ||
err = mgr.Add(tlsFileWatcher) | ||
if err != nil { | ||
return err | ||
} | ||
listener = tls.NewListener(listener, config) | ||
} | ||
|
||
shutdownTimeout := 30 * time.Second | ||
|
||
catalogServer := server.Server{ | ||
Kind: "catalogs", | ||
Server: &http.Server{ | ||
Addr: cfg.CatalogAddr, | ||
Handler: catalogdmetrics.AddMetricsToHandler(cfg.LocalStorage.StorageServerHandler()), | ||
ReadTimeout: 5 * time.Second, | ||
// TODO: Revert this to 10 seconds if/when the API | ||
// evolves to have significantly smaller responses | ||
WriteTimeout: 5 * time.Minute, | ||
}, | ||
ShutdownTimeout: &shutdownTimeout, | ||
Listener: listener, | ||
} | ||
|
||
err = mgr.Add(&catalogServer) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |