|
| 1 | +/* |
| 2 | +Copyright 2024 k0s authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package kubeconfig_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/k0sproject/k0s/cmd" |
| 28 | + "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1" |
| 29 | + "github.com/k0sproject/k0s/pkg/config" |
| 30 | + |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/client-go/tools/clientcmd" |
| 33 | + "k8s.io/client-go/tools/clientcmd/api" |
| 34 | + "sigs.k8s.io/yaml" |
| 35 | + |
| 36 | + "github.com/stretchr/testify/assert" |
| 37 | + "github.com/stretchr/testify/require" |
| 38 | +) |
| 39 | + |
| 40 | +func TestAdmin(t *testing.T) { |
| 41 | + dataDir := t.TempDir() |
| 42 | + |
| 43 | + configPath := filepath.Join(dataDir, "k0s.yaml") |
| 44 | + writeYAML(t, configPath, &v1beta1.ClusterConfig{ |
| 45 | + TypeMeta: metav1.TypeMeta{APIVersion: v1beta1.SchemeGroupVersion.String(), Kind: v1beta1.ClusterConfigKind}, |
| 46 | + Spec: &v1beta1.ClusterSpec{API: &v1beta1.APISpec{ |
| 47 | + Port: 65432, ExternalAddress: "not-here.example.com", |
| 48 | + }}, |
| 49 | + }) |
| 50 | + |
| 51 | + adminConfPath := filepath.Join(dataDir, "admin.conf") |
| 52 | + require.NoError(t, clientcmd.WriteToFile(api.Config{ |
| 53 | + Clusters: map[string]*api.Cluster{ |
| 54 | + t.Name(): {Server: "https://localhost:65432"}, |
| 55 | + }, |
| 56 | + }, adminConfPath)) |
| 57 | + |
| 58 | + rtConfigPath := filepath.Join(dataDir, "run", "k0s.yaml") |
| 59 | + writeYAML(t, rtConfigPath, &config.RuntimeConfig{ |
| 60 | + TypeMeta: metav1.TypeMeta{APIVersion: v1beta1.SchemeGroupVersion.String(), Kind: config.RuntimeConfigKind}, |
| 61 | + Spec: &config.RuntimeConfigSpec{K0sVars: &config.CfgVars{ |
| 62 | + AdminKubeConfigPath: adminConfPath, |
| 63 | + DataDir: dataDir, |
| 64 | + RuntimeConfigPath: rtConfigPath, |
| 65 | + StartupConfigPath: configPath, |
| 66 | + }}, |
| 67 | + }) |
| 68 | + |
| 69 | + var stdout bytes.Buffer |
| 70 | + var stderr strings.Builder |
| 71 | + underTest := cmd.NewRootCmd() |
| 72 | + underTest.SetArgs([]string{"kubeconfig", "--data-dir", dataDir, "admin"}) |
| 73 | + underTest.SetOut(&stdout) |
| 74 | + underTest.SetErr(&stderr) |
| 75 | + |
| 76 | + assert.NoError(t, underTest.Execute()) |
| 77 | + |
| 78 | + assert.Empty(t, stderr.String()) |
| 79 | + |
| 80 | + adminConf, err := clientcmd.Load(stdout.Bytes()) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + if theCluster, ok := adminConf.Clusters[t.Name()]; assert.True(t, ok) { |
| 84 | + assert.Equal(t, "https://not-here.example.com:65432", theCluster.Server) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestAdmin_NoAdminConfig(t *testing.T) { |
| 89 | + dataDir := t.TempDir() |
| 90 | + |
| 91 | + configPath := filepath.Join(dataDir, "k0s.yaml") |
| 92 | + adminConfPath := filepath.Join(dataDir, "admin.conf") |
| 93 | + rtConfigPath := filepath.Join(dataDir, "run", "k0s.yaml") |
| 94 | + writeYAML(t, rtConfigPath, &config.RuntimeConfig{ |
| 95 | + TypeMeta: metav1.TypeMeta{APIVersion: v1beta1.SchemeGroupVersion.String(), Kind: config.RuntimeConfigKind}, |
| 96 | + Spec: &config.RuntimeConfigSpec{K0sVars: &config.CfgVars{ |
| 97 | + AdminKubeConfigPath: adminConfPath, |
| 98 | + DataDir: dataDir, |
| 99 | + RuntimeConfigPath: rtConfigPath, |
| 100 | + StartupConfigPath: configPath, |
| 101 | + }}, |
| 102 | + }) |
| 103 | + |
| 104 | + var stdout, stderr strings.Builder |
| 105 | + underTest := cmd.NewRootCmd() |
| 106 | + underTest.SetArgs([]string{"kubeconfig", "--data-dir", dataDir, "admin"}) |
| 107 | + underTest.SetOut(&stdout) |
| 108 | + underTest.SetErr(&stderr) |
| 109 | + |
| 110 | + assert.Error(t, underTest.Execute()) |
| 111 | + |
| 112 | + assert.Empty(t, stdout.String()) |
| 113 | + msg := fmt.Sprintf("admin config %q not found, check if the control plane is initialized on this node", adminConfPath) |
| 114 | + assert.Equal(t, "Error: "+msg+"\n", stderr.String()) |
| 115 | +} |
| 116 | + |
| 117 | +func writeYAML(t *testing.T, path string, data any) { |
| 118 | + bytes, err := yaml.Marshal(data) |
| 119 | + require.NoError(t, err) |
| 120 | + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755)) |
| 121 | + require.NoError(t, os.WriteFile(path, bytes, 0644)) |
| 122 | +} |
0 commit comments