Skip to content

Commit

Permalink
Add credential provider option to kubeadm
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmasi committed Apr 25, 2024
1 parent df71fa7 commit 0efbe25
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 290 deletions.
38 changes: 38 additions & 0 deletions cluster/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kubeadm

import (
"fmt"
"os"
"strings"

"github.com/openconfig/kne/exec/run"
)

const (
kubeadmFlagPath = "/var/lib/kubelet/kubeadm-flags.env"
)

// EnableCredentialProvider enables a credential provider according
// to the specified config file on the kubelet.
func EnableCredentialProvider(cfgPath string) error {
if err := run.LogCommand("sudo", "kubeadm", "upgrade", "node", "phase", "kubelet-config"); err != nil {
return err
}
b, err := os.ReadFile(kubeadmFlagPath)
if err != nil {
return err
}
s, _ := strings.CutSuffix(string(b), `"`)
s = fmt.Sprintf(`%s --image-credential-provider-config=%s --image-credential-provider-bin-dir=/etc/kubernetes/bin"`, s, cfgPath)
f, err := os.Create(kubeadmFlagPath)
if err != nil {
return err
}
if _, err := f.WriteString(s); err != nil {
return err
}
if err := run.LogCommand("sudo", "systemctl", "restart", "kubelet"); err != nil {
return err
}
return nil
}
6 changes: 6 additions & 0 deletions controller/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

log "github.com/golang/glog"
"github.com/openconfig/kne/cluster/kubeadm"
"github.com/openconfig/kne/deploy"
"github.com/openconfig/kne/exec/run"
cpb "github.com/openconfig/kne/proto/controller"
Expand Down Expand Up @@ -588,6 +589,11 @@ func (s *server) JoinCluster(ctx context.Context, req *cpb.JoinClusterRequest) (
if err := run.LogCommand("sudo", args...); err != nil {
return nil, status.Errorf(codes.Internal, "failed to join kubeadm cluster: %v", err)
}
if req.GetCredentialProviderConfig() != "" {
if err := kubeadm.EnableCredentialProvider(req.GetCredentialProviderConfig()); err != nil {
return nil, err
}
}
return &cpb.JoinClusterResponse{}, nil
}

Expand Down
9 changes: 8 additions & 1 deletion deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/openconfig/gnmi/errlist"
metallbclientv1 "github.com/openconfig/kne/api/metallb/clientset/v1beta1"
"github.com/openconfig/kne/cluster/kind"
"github.com/openconfig/kne/cluster/kubeadm"
"github.com/openconfig/kne/events"
"github.com/openconfig/kne/exec/run"
"github.com/openconfig/kne/load"
Expand Down Expand Up @@ -420,6 +421,7 @@ type KubeadmSpec struct {
PodNetworkCIDR string `yaml:"podNetworkCIDR"`
PodNetworkAddOnManifest string `yaml:"podNetworkAddOnManifest" kne:"yaml"`
PodNetworkAddOnManifestData []byte
CredentialProviderConfig string `yaml:"credentialProviderConfig" kne:"yaml"`
TokenTTL string `yaml:"tokenTTL"`
Network string `yaml:"network"`
AllowControlPlaneScheduling bool `yaml:"allowControlPlaneScheduling"`
Expand Down Expand Up @@ -493,7 +495,12 @@ func (k *KubeadmSpec) Deploy(ctx context.Context) error {
return err
}
}

// If credential provider config provided, apply it.
if k.CredentialProviderConfig != "" {
if err := kubeadm.EnableCredentialProvider(k.CredentialProviderConfig); err != nil {
return err
}
}
// Create a new docker network if not specified.
if k.Network == "" {
k.Network = "kne-kubeadm-" + uuid.New()
Expand Down
11 changes: 0 additions & 11 deletions kind/kind-no-cni.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions manifests/kube/credential-provider-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
kind: CredentialProviderConfig
apiVersion: kubelet.config.k8s.io/v1
providers:
- name: auth-provider-gcp
apiVersion: credentialprovider.kubelet.k8s.io/v1
matchImages:
- "container.cloud.google.com"
- "gcr.io"
- "*.gcr.io"
- "*.pkg.dev"
args:
- get-credentials
- --v=3
defaultCacheDuration: 1m
2 changes: 2 additions & 0 deletions proto/controller.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ message KubeadmSpec {
string token_ttl = 4;
string network = 5; // name of the docker network to use for ingress
bool allow_control_plane_scheduling = 6;
string credential_provider_config = 7;
}

// External cluster specifications
Expand Down Expand Up @@ -286,6 +287,7 @@ message JoinClusterRequest {
string token = 2;
string discovery_token_ca_cert_hash = 3;
string cri_socket = 4;
string credential_provider_config = 5;
}

// Returns join cluster response.
Expand Down
Loading

0 comments on commit 0efbe25

Please sign in to comment.