Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run node certificate manager in hybrid overlay #3939

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go-controller/cmd/ovnkube/ovnkube.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func startOvnKube(ctx *cli.Context, cancel context.CancelFunc) error {
}()

if config.Kubernetes.BootstrapKubeconfig != "" {
if err := util.StartNodeCertificateManager(ctx.Context, ovnKubeStartWg, &config.Kubernetes); err != nil {
if err := util.StartNodeCertificateManager(ctx.Context, ovnKubeStartWg, os.Getenv("K8S_NODE"), &config.Kubernetes); err != nil {
return fmt.Errorf("failed to start the node certificate manager: %w", err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"

"github.com/urfave/cli/v2"
"k8s.io/client-go/tools/clientcmd"

"github.com/ovn-org/ovn-kubernetes/go-controller/hybrid-overlay/pkg/controller"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/config"
Expand Down Expand Up @@ -99,7 +100,29 @@ func runHybridOverlay(ctx *cli.Context) error {
return fmt.Errorf("missing node name; use the 'node' flag to provide one")
}

clientset, err := util.NewKubernetesClientset(&config.Kubernetes)
wg := &sync.WaitGroup{}
clientCfg := config.Kubernetes
if config.Kubernetes.BootstrapKubeconfig != "" {
if err := util.StartNodeCertificateManager(ctx.Context, wg, nodeName, &config.Kubernetes); err != nil {
return fmt.Errorf("failed to start the node certificate manager: %w", err)
}

bootstrapConfig, err := clientcmd.BuildConfigFromFlags("", config.Kubernetes.BootstrapKubeconfig)
if err != nil {
return err
}
// Copy the APIServer and CAData from the bootstrap kubeconfig
clientCfg.APIServer = bootstrapConfig.Host
clientCfg.CAData = bootstrapConfig.CAData
if bootstrapConfig.CAFile != "" {
bytes, err := os.ReadFile(bootstrapConfig.CAFile)
if err != nil {
return err
}
clientCfg.CAData = bytes
}
}
clientset, err := util.NewKubernetesClientset(&clientCfg)
if err != nil {
return err
}
Expand All @@ -121,7 +144,6 @@ func runHybridOverlay(ctx *cli.Context) error {
}

f.Start(stopChan)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
Expand Down
14 changes: 6 additions & 8 deletions go-controller/pkg/util/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"net"
"os"
"path"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -208,8 +207,8 @@ func newKubernetesRestConfig(conf *config.KubernetesConfig) (*rest.Config, error
// uses the current context in kubeconfig
kconfig, err = clientcmd.BuildConfigFromFlags("", conf.Kubeconfig)
} else if strings.HasPrefix(conf.APIServer, "https") {
if conf.Token == "" || len(conf.CAData) == 0 {
return nil, fmt.Errorf("TLS-secured apiservers require token and CA certificate")
if (conf.Token == "" && conf.CertDir == "") || len(conf.CAData) == 0 {
return nil, fmt.Errorf("TLS-secured apiservers require token/cert and CA certificate")
}
if _, err := cert.NewPoolFromBytes(conf.CAData); err != nil {
return nil, err
Expand All @@ -224,8 +223,8 @@ func newKubernetesRestConfig(conf *config.KubernetesConfig) (*rest.Config, error
kconfig = &rest.Config{
Host: conf.APIServer,
TLSClientConfig: rest.TLSClientConfig{
KeyFile: path.Join(conf.CertDir, certNamePrefix+"-current.pem"),
CertFile: path.Join(conf.CertDir, certNamePrefix+"-current.pem"),
KeyFile: filepath.Join(conf.CertDir, certNamePrefix+"-current.pem"),
CertFile: filepath.Join(conf.CertDir, certNamePrefix+"-current.pem"),
CAData: conf.CAData,
},
}
Expand Down Expand Up @@ -254,10 +253,9 @@ func newKubernetesRestConfig(conf *config.KubernetesConfig) (*rest.Config, error
// StartNodeCertificateManager manages the creation and rotation of the node-specific client certificate.
// When there is no existing certificate, it will use the BootstrapKubeconfig kubeconfig to create a CSR and it will
// wait for the certificate before returning.
func StartNodeCertificateManager(ctx context.Context, wg *sync.WaitGroup, conf *config.KubernetesConfig) error {
nodeName := os.Getenv("K8S_NODE")
func StartNodeCertificateManager(ctx context.Context, wg *sync.WaitGroup, nodeName string, conf *config.KubernetesConfig) error {
if nodeName == "" {
return fmt.Errorf("failed to get the node name required for the certificate from K8S_NODE env")
return fmt.Errorf("the provided node name cannot be empty")
}
defaultKConfig, err := newKubernetesRestConfig(conf)
if err != nil {
Expand Down