-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
8b6ec82
commit 26d06f3
Showing
5 changed files
with
160 additions
and
4 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
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,22 @@ | ||
package updateclientsecret | ||
|
||
import ( | ||
cli "github.com/rancher/wrangler-cli" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/rancher/rancherd/pkg/rancher" | ||
) | ||
|
||
func NewUpdateClientSecret() *cobra.Command { | ||
return cli.Command(&UpdateClientSecret{}, cobra.Command{ | ||
Short: "Update cluster client secret to have API Server URL and CA Certs configured", | ||
}) | ||
} | ||
|
||
type UpdateClientSecret struct { | ||
Kubeconfig string `usage:"Kubeconfig file" env:"KUBECONFIG"` | ||
} | ||
|
||
func (s *UpdateClientSecret) Run(cmd *cobra.Command, args []string) error { | ||
return rancher.UpdateClientSecret(cmd.Context(), &rancher.Options{Kubeconfig: s.Kubeconfig}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package rancher | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/rancher/rancherd/pkg/kubectl" | ||
) | ||
|
||
const ( | ||
rancherSettingInternalServerURL = "internal-server-url" | ||
rancherSettingInternalCACerts = "internal-cacerts" | ||
clusterClientSecret = "local-kubeconfig" | ||
clusterNamespace = "fleet-local" | ||
) | ||
|
||
type Options struct { | ||
Kubeconfig string | ||
} | ||
|
||
// Update cluster client secret (fleet-local/local-kubeconfig): | ||
// 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 | ||
func UpdateClientSecret(ctx context.Context, opts *Options) error { | ||
if opts == nil { | ||
opts = &Options{} | ||
} | ||
|
||
kubeconfig, err := kubectl.GetKubeconfig(opts.Kubeconfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
conf, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := dynamic.NewForConfigOrDie(conf) | ||
settingClient := client.Resource(schema.GroupVersionResource{ | ||
Group: "management.cattle.io", | ||
Version: "v3", | ||
Resource: "settings", | ||
}) | ||
|
||
internalServerURLSetting, err := settingClient.Get(ctx, rancherSettingInternalServerURL, v1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
internalServerURL := internalServerURLSetting.Object["value"].(string) | ||
logrus.Infof("Rancher setting %s is %q", rancherSettingInternalServerURL, internalServerURL) | ||
|
||
internalCACertSetting, err := settingClient.Get(ctx, rancherSettingInternalCACerts, v1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
internalCACerts := internalCACertSetting.Object["value"].(string) | ||
logrus.Infof("Rancher setting %s is %q", rancherSettingInternalCACerts, internalCACerts) | ||
|
||
if internalServerURL == "" || internalCACerts == "" { | ||
return fmt.Errorf("both %s and %s settings must be configured", rancherSettingInternalCACerts, rancherSettingInternalCACerts) | ||
} | ||
|
||
k8s, err := kubernetes.NewForConfig(conf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
secret, err := k8s.CoreV1().Secrets(clusterNamespace).Get(ctx, clusterClientSecret, v1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
toUpdate := secret.DeepCopy() | ||
toUpdate.Data["apiServerURL"] = []byte(internalServerURL) | ||
toUpdate.Data["apiServerCA"] = []byte(internalCACerts) | ||
_, err = k8s.CoreV1().Secrets(clusterNamespace).Update(ctx, toUpdate, v1.UpdateOptions{}) | ||
|
||
if err == nil { | ||
fmt.Println("Cluster client secret is updated.") | ||
} | ||
|
||
return 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