-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add credential provider option to kubeadm (#533)
* Add credential provider option to kubeadm * add unit test * fix string manipulation
- Loading branch information
Showing
8 changed files
with
486 additions
and
290 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package kubeadm | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/openconfig/kne/exec/run" | ||
log "k8s.io/klog/v2" | ||
) | ||
|
||
var ( | ||
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 { | ||
log.Infof("Enabling credential provider with config %q...", cfgPath) | ||
if _, err := os.Stat(cfgPath); err != nil { | ||
return fmt.Errorf("config file not found: %v", err) | ||
} | ||
if err := run.LogCommand("sudo", "kubeadm", "upgrade", "node", "phase", "kubelet-config"); err != nil { | ||
return err | ||
} | ||
b, err := os.ReadFile(kubeadmFlagPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to read kubeadm flag file: %v", err) | ||
} | ||
s, ok := strings.CutSuffix(string(b), "\"\n") | ||
if !ok { | ||
return fmt.Errorf("kubeadm flag file %q does not have expected contents: %q", kubeadmFlagPath, s) | ||
} | ||
s = fmt.Sprintf("%s --image-credential-provider-config=%s --image-credential-provider-bin-dir=/etc/kubernetes/bin\"\n", s, cfgPath) | ||
f, err := os.CreateTemp("", "kne-kubeadm-flag.env") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.RemoveAll(f.Name()) | ||
if _, err := f.WriteString(s); err != nil { | ||
return err | ||
} | ||
if err := run.LogCommand("sudo", "cp", f.Name(), kubeadmFlagPath); err != nil { | ||
return err | ||
} | ||
if err := run.LogCommand("sudo", "systemctl", "restart", "kubelet"); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
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,96 @@ | ||
package kubeadm | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/openconfig/gnmi/errdiff" | ||
kexec "github.com/openconfig/kne/exec" | ||
fexec "github.com/openconfig/kne/exec/fake" | ||
) | ||
|
||
func TestEnableCredentialProvider(t *testing.T) { | ||
f, err := os.CreateTemp(t.TempDir(), "flags.env") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp file for test: %v", err) | ||
} | ||
if _, err := f.WriteString("KUBELET_KUBEADM_ARGS=\"--container-runtime-endpoint=unix:///var/run/cri-dockerd.sock\"\n"); err != nil { | ||
t.Fatalf("Failed to write temp file for test: %v", err) | ||
} | ||
|
||
origKubeadmFlagPath := kubeadmFlagPath | ||
defer func() { | ||
kubeadmFlagPath = origKubeadmFlagPath | ||
}() | ||
kubeadmFlagPath = f.Name() | ||
|
||
cfg, err := os.CreateTemp(t.TempDir(), "cfg.yaml") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp cfg file for test: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
desc string | ||
cfgPath string | ||
resp []fexec.Response | ||
wantErr string | ||
}{{ | ||
desc: "success", | ||
cfgPath: cfg.Name(), | ||
resp: []fexec.Response{ | ||
{Cmd: "sudo", Args: []string{"kubeadm", "upgrade", "node", "phase", "kubelet-config"}}, | ||
{Cmd: "sudo", Args: []string{"cp", ".*", kubeadmFlagPath}}, | ||
{Cmd: "sudo", Args: []string{"systemctl", "restart", "kubelet"}}, | ||
}, | ||
}, { | ||
desc: "config file not found", | ||
cfgPath: "dne", | ||
wantErr: "config file not found", | ||
}, { | ||
desc: "failed to upgrade kubelet", | ||
cfgPath: cfg.Name(), | ||
resp: []fexec.Response{ | ||
{Cmd: "sudo", Args: []string{"kubeadm", "upgrade", "node", "phase", "kubelet-config"}, Err: "failed to upgrade kubelet"}, | ||
}, | ||
wantErr: "failed to upgrade kubelet", | ||
}, { | ||
desc: "failed to copy flag config", | ||
cfgPath: cfg.Name(), | ||
resp: []fexec.Response{ | ||
{Cmd: "sudo", Args: []string{"kubeadm", "upgrade", "node", "phase", "kubelet-config"}}, | ||
{Cmd: "sudo", Args: []string{"cp", ".*", kubeadmFlagPath}, Err: "failed to copy"}, | ||
}, | ||
wantErr: "failed to copy", | ||
}, { | ||
desc: "failed to restart kubelet", | ||
cfgPath: cfg.Name(), | ||
resp: []fexec.Response{ | ||
{Cmd: "sudo", Args: []string{"kubeadm", "upgrade", "node", "phase", "kubelet-config"}}, | ||
{Cmd: "sudo", Args: []string{"cp", ".*", kubeadmFlagPath}}, | ||
{Cmd: "sudo", Args: []string{"systemctl", "restart", "kubelet"}, Err: "failed to restart kubelet"}, | ||
}, | ||
wantErr: "failed to restart kubelet", | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
fexec.LogCommand = func(s string) { | ||
t.Logf("%s: %s", tt.desc, s) | ||
} | ||
cmds := fexec.Commands(tt.resp) | ||
kexec.Command = cmds.Command | ||
defer checkCmds(t, cmds) | ||
|
||
err := EnableCredentialProvider(tt.cfgPath) | ||
if s := errdiff.Substring(err, tt.wantErr); s != "" { | ||
t.Fatalf("unexpected error: %s", s) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func checkCmds(t *testing.T, cmds *fexec.Command) { | ||
t.Helper() | ||
if err := cmds.Done(); err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,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 |
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
Oops, something went wrong.