Skip to content

Commit 7267ad0

Browse files
committed
Add direct connection arguments to mgmt cluster
This adds new arguments to allow the operator to connect to cluster api mgmt cluster without a kubeconfig file, which is more practical for containerized environments. It includes flags for the API URL, bearer token, and CA data, while retaining backward compatibility with the existing file-based method. Signed-off-by: Mamduh Alassi <[email protected]>
1 parent a72bd26 commit 7267ad0

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

pkg/operator/operator.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/samber/lo"
2424
"k8s.io/client-go/kubernetes/scheme"
25+
"k8s.io/client-go/rest"
2526
"k8s.io/client-go/tools/clientcmd"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
"sigs.k8s.io/controller-runtime/pkg/cluster"
@@ -70,11 +71,12 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
7071
}
7172

7273
func buildManagementClusterKubeClient(ctx context.Context, operator *operator.Operator) (client.Client, error) {
73-
if options.FromContext(ctx).ClusterAPIKubeConfigFile != "" {
74-
clusterAPIKubeConfig, err := clientcmd.BuildConfigFromFlags("", options.FromContext(ctx).ClusterAPIKubeConfigFile)
75-
if err != nil {
76-
return nil, err
77-
}
74+
clusterAPIKubeConfig, err := buildClusterCAPIKubeConfig(ctx)
75+
if err != nil {
76+
return nil, err
77+
}
78+
79+
if clusterAPIKubeConfig != nil {
7880
mgmtCluster, err := cluster.New(clusterAPIKubeConfig, func(o *cluster.Options) {
7981
o.Scheme = operator.GetScheme()
8082
})
@@ -88,3 +90,27 @@ func buildManagementClusterKubeClient(ctx context.Context, operator *operator.Op
8890
}
8991
return operator.GetClient(), nil
9092
}
93+
94+
func buildClusterCAPIKubeConfig(ctx context.Context) (*rest.Config, error) {
95+
kubeConfigFile := options.FromContext(ctx).ClusterAPIKubeConfigFile
96+
if kubeConfigFile != "" {
97+
return clientcmd.BuildConfigFromFlags("", kubeConfigFile)
98+
}
99+
100+
url := options.FromContext(ctx).ClusterAPIUrl
101+
token := options.FromContext(ctx).ClusterAPIToken
102+
caData := options.FromContext(ctx).ClusterAPICertificateAuthorityData
103+
skipTLSVerify := options.FromContext(ctx).ClusterAPISkipTlsVerify
104+
if url != "" {
105+
return &rest.Config{
106+
Host: url,
107+
BearerToken: token,
108+
TLSClientConfig: rest.TLSClientConfig{
109+
CAData: []byte(caData),
110+
Insecure: skipTLSVerify,
111+
},
112+
}, nil
113+
}
114+
115+
return nil, nil
116+
}

pkg/operator/options/options.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ func init() {
3333
type optionsKey struct{}
3434

3535
type Options struct {
36-
ClusterAPIKubeConfigFile string
36+
ClusterAPIKubeConfigFile string
37+
ClusterAPIUrl string
38+
ClusterAPIToken string
39+
ClusterAPICertificateAuthorityData string
40+
ClusterAPISkipTlsVerify bool
3741
}
3842

3943
func (o *Options) AddFlags(fs *karpoptions.FlagSet) {
4044
fs.StringVar(&o.ClusterAPIKubeConfigFile, "cluster-api-kubeconfig", "", "The path to the cluster api manager cluster kubeconfig file. Defaults to service account credentials if not specified.")
45+
fs.StringVar(&o.ClusterAPIUrl, "cluster-api-url", "", "The url of the cluster api manager cluster")
46+
fs.StringVar(&o.ClusterAPIToken, "cluster-api-token", "", "The Bearer token for authentication of the cluster api manager cluster")
47+
fs.StringVar(&o.ClusterAPICertificateAuthorityData, "cluster-api-certificate-authority-data", "", "The cert certificate authority of the cluster api manager cluster")
48+
fs.BoolVar(&o.ClusterAPISkipTlsVerify, "cluster-api-skip-tls-verify", false, "Skip the check for certificate for validity of the cluster api manager cluster. This will make HTTPS connections insecure")
4149
}
4250

4351
func (o *Options) Parse(fs *karpoptions.FlagSet, args ...string) error {

0 commit comments

Comments
 (0)