Skip to content

Commit 26d06f3

Browse files
bk201guangbochen
authored andcommitted
Update cluster client secret fleet-local/local-kubeconfig
Update these fields: apiServerURL: value of Rancher setting "internal-server-url". apiServerCA: value of Rancher setting "internal-cacerts". Fleet needs these values to be set after Rancher v2.7.5 to provision a local cluster Signed-off-by: Kiefer Chang <[email protected]>
1 parent 8b6ec82 commit 26d06f3

File tree

5 files changed

+160
-4
lines changed

5 files changed

+160
-4
lines changed

cmd/rancherd/main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package main
22

33
import (
4+
cli "github.com/rancher/wrangler-cli"
5+
"github.com/spf13/cobra"
6+
47
"github.com/rancher/rancherd/cmd/rancherd/bootstrap"
58
"github.com/rancher/rancherd/cmd/rancherd/gettoken"
69
"github.com/rancher/rancherd/cmd/rancherd/gettpmhash"
710
"github.com/rancher/rancherd/cmd/rancherd/info"
811
"github.com/rancher/rancherd/cmd/rancherd/probe"
912
"github.com/rancher/rancherd/cmd/rancherd/resetadmin"
1013
"github.com/rancher/rancherd/cmd/rancherd/retry"
14+
"github.com/rancher/rancherd/cmd/rancherd/updateclientsecret"
1115
"github.com/rancher/rancherd/cmd/rancherd/upgrade"
12-
cli "github.com/rancher/wrangler-cli"
13-
"github.com/spf13/cobra"
1416
)
1517

1618
type Rancherd struct {
@@ -33,6 +35,7 @@ func main() {
3335
upgrade.NewUpgrade(),
3436
info.NewInfo(),
3537
gettpmhash.NewGetTPMHash(),
38+
updateclientsecret.NewUpdateClientSecret(),
3639
)
3740
cli.Main(root)
3841
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package updateclientsecret
2+
3+
import (
4+
cli "github.com/rancher/wrangler-cli"
5+
"github.com/spf13/cobra"
6+
7+
"github.com/rancher/rancherd/pkg/rancher"
8+
)
9+
10+
func NewUpdateClientSecret() *cobra.Command {
11+
return cli.Command(&UpdateClientSecret{}, cobra.Command{
12+
Short: "Update cluster client secret to have API Server URL and CA Certs configured",
13+
})
14+
}
15+
16+
type UpdateClientSecret struct {
17+
Kubeconfig string `usage:"Kubeconfig file" env:"KUBECONFIG"`
18+
}
19+
20+
func (s *UpdateClientSecret) Run(cmd *cobra.Command, args []string) error {
21+
return rancher.UpdateClientSecret(cmd.Context(), &rancher.Options{Kubeconfig: s.Kubeconfig})
22+
}

pkg/plan/bootstrap.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/rancher/system-agent/pkg/applyinator"
8+
79
"github.com/rancher/rancherd/pkg/config"
810
"github.com/rancher/rancherd/pkg/discovery"
911
"github.com/rancher/rancherd/pkg/join"
@@ -14,7 +16,6 @@ import (
1416
"github.com/rancher/rancherd/pkg/resources"
1517
"github.com/rancher/rancherd/pkg/runtime"
1618
"github.com/rancher/rancherd/pkg/versions"
17-
"github.com/rancher/system-agent/pkg/applyinator"
1819
)
1920

2021
type plan applyinator.Plan
@@ -106,6 +107,14 @@ func (p *plan) addInstructions(cfg *config.Config, dataDir string) error {
106107
return err
107108
}
108109

110+
if err := p.addInstruction(rancher.ToWaitClusterClientSecretInstruction(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion)); err != nil {
111+
return err
112+
}
113+
114+
if err := p.addInstruction(rancher.ToUpdateClientSecretInstruction(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion)); err != nil {
115+
return err
116+
}
117+
109118
if err := p.addInstruction(resources.ToInstruction(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion, dataDir)); err != nil {
110119
return err
111120
}

pkg/rancher/cluster.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package rancher
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/sirupsen/logrus"
8+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/runtime/schema"
10+
"k8s.io/client-go/dynamic"
11+
"k8s.io/client-go/kubernetes"
12+
"k8s.io/client-go/tools/clientcmd"
13+
14+
"github.com/rancher/rancherd/pkg/kubectl"
15+
)
16+
17+
const (
18+
rancherSettingInternalServerURL = "internal-server-url"
19+
rancherSettingInternalCACerts = "internal-cacerts"
20+
clusterClientSecret = "local-kubeconfig"
21+
clusterNamespace = "fleet-local"
22+
)
23+
24+
type Options struct {
25+
Kubeconfig string
26+
}
27+
28+
// Update cluster client secret (fleet-local/local-kubeconfig):
29+
// apiServerURL: value of Rancher setting "internal-server-url"
30+
// apiServerCA: value of Rancher setting "internal-cacerts"
31+
// Fleet needs these values to be set after Rancher v2.7.5 to provision a local cluster
32+
func UpdateClientSecret(ctx context.Context, opts *Options) error {
33+
if opts == nil {
34+
opts = &Options{}
35+
}
36+
37+
kubeconfig, err := kubectl.GetKubeconfig(opts.Kubeconfig)
38+
if err != nil {
39+
return err
40+
}
41+
42+
conf, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
43+
if err != nil {
44+
return err
45+
}
46+
47+
client := dynamic.NewForConfigOrDie(conf)
48+
settingClient := client.Resource(schema.GroupVersionResource{
49+
Group: "management.cattle.io",
50+
Version: "v3",
51+
Resource: "settings",
52+
})
53+
54+
internalServerURLSetting, err := settingClient.Get(ctx, rancherSettingInternalServerURL, v1.GetOptions{})
55+
if err != nil {
56+
return err
57+
}
58+
internalServerURL := internalServerURLSetting.Object["value"].(string)
59+
logrus.Infof("Rancher setting %s is %q", rancherSettingInternalServerURL, internalServerURL)
60+
61+
internalCACertSetting, err := settingClient.Get(ctx, rancherSettingInternalCACerts, v1.GetOptions{})
62+
if err != nil {
63+
return err
64+
}
65+
internalCACerts := internalCACertSetting.Object["value"].(string)
66+
logrus.Infof("Rancher setting %s is %q", rancherSettingInternalCACerts, internalCACerts)
67+
68+
if internalServerURL == "" || internalCACerts == "" {
69+
return fmt.Errorf("both %s and %s settings must be configured", rancherSettingInternalCACerts, rancherSettingInternalCACerts)
70+
}
71+
72+
k8s, err := kubernetes.NewForConfig(conf)
73+
if err != nil {
74+
return err
75+
}
76+
77+
secret, err := k8s.CoreV1().Secrets(clusterNamespace).Get(ctx, clusterClientSecret, v1.GetOptions{})
78+
if err != nil {
79+
return err
80+
}
81+
82+
toUpdate := secret.DeepCopy()
83+
toUpdate.Data["apiServerURL"] = []byte(internalServerURL)
84+
toUpdate.Data["apiServerCA"] = []byte(internalCACerts)
85+
_, err = k8s.CoreV1().Secrets(clusterNamespace).Update(ctx, toUpdate, v1.UpdateOptions{})
86+
87+
if err == nil {
88+
fmt.Println("Cluster client secret is updated.")
89+
}
90+
91+
return err
92+
}

pkg/rancher/wait.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/rancher/system-agent/pkg/applyinator"
8+
79
"github.com/rancher/rancherd/pkg/kubectl"
810
"github.com/rancher/rancherd/pkg/self"
9-
"github.com/rancher/system-agent/pkg/applyinator"
1011
)
1112

1213
func ToWaitRancherInstruction(imageOverride, systemDefaultRegistry, k8sVersion string) (*applyinator.Instruction, error) {
@@ -65,3 +66,32 @@ func ToWaitSUCPlanInstruction(imageOverride, systemDefaultRegistry, k8sVersion s
6566
Command: cmd,
6667
}, nil
6768
}
69+
70+
func ToWaitClusterClientSecretInstruction(imageOverride, systemDefaultRegistry, k8sVersion string) (*applyinator.Instruction, error) {
71+
cmd, err := self.Self()
72+
if err != nil {
73+
return nil, fmt.Errorf("resolving location of %s: %w", os.Args[0], err)
74+
}
75+
return &applyinator.Instruction{
76+
Name: "wait-cluster-client-secret-resolved",
77+
SaveOutput: true,
78+
Args: []string{"retry", kubectl.Command(k8sVersion), "-n", clusterNamespace, "get",
79+
"secret", clusterClientSecret},
80+
Env: kubectl.Env(k8sVersion),
81+
Command: cmd,
82+
}, nil
83+
}
84+
85+
func ToUpdateClientSecretInstruction(imageOverride, systemDefaultRegistry, k8sVersion string) (*applyinator.Instruction, error) {
86+
cmd, err := self.Self()
87+
if err != nil {
88+
return nil, fmt.Errorf("resolving location of %s: %w", os.Args[0], err)
89+
}
90+
return &applyinator.Instruction{
91+
Name: "update-client-secret",
92+
SaveOutput: true,
93+
Args: []string{"update-client-secret"},
94+
Env: kubectl.Env(k8sVersion),
95+
Command: cmd,
96+
}, nil
97+
}

0 commit comments

Comments
 (0)