github.com/oam-dev/cluster-gateway@v1.9.0/e2e/env/prepare/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1"
     7  	"github.com/oam-dev/cluster-gateway/pkg/common"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/ghodss/yaml"
    12  	corev1 "k8s.io/api/core/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  	"k8s.io/client-go/tools/clientcmd"
    15  	"k8s.io/klog/v2"
    16  )
    17  
    18  func main() {
    19  
    20  	var clusterName string
    21  	var secretNamespace string
    22  	var dryRun bool
    23  
    24  	flag.StringVar(&secretNamespace, "secret-namespace", "open-cluster-management-credentials",
    25  		"Namespace of the cluster secret.")
    26  	flag.StringVar(&clusterName, "cluster-name", "loopback",
    27  		"Target name of the secret.")
    28  	flag.BoolVar(&dryRun, "dry-run", false,
    29  		"Whether to dry run")
    30  	flag.Parse()
    31  
    32  	kubeconfigPath := os.Getenv("KUBECONFIG")
    33  	if len(kubeconfigPath) == 0 {
    34  		kubeconfigPath = filepath.Join(os.Getenv("HOME"), ".kube", "config")
    35  	}
    36  	restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
    37  	if err != nil {
    38  		klog.Fatal(err)
    39  	}
    40  
    41  	secret := &corev1.Secret{
    42  		TypeMeta: metav1.TypeMeta{
    43  			APIVersion: "v1",
    44  			Kind:       "Secret",
    45  		},
    46  		ObjectMeta: metav1.ObjectMeta{
    47  			Namespace: secretNamespace,
    48  			Name:      clusterName,
    49  			Labels:    map[string]string{},
    50  		},
    51  		Data: map[string][]byte{
    52  			"ca.crt":   restConfig.CAData,
    53  			"endpoint": []byte("https://kubernetes.default.svc.cluster.local:443"),
    54  		},
    55  	}
    56  	if len(restConfig.BearerToken) > 0 {
    57  		// TODO
    58  	} else {
    59  		secret.Labels[common.LabelKeyClusterCredentialType] = string(v1alpha1.CredentialTypeX509Certificate)
    60  		secret.Data["tls.crt"] = restConfig.CertData
    61  		secret.Data["tls.key"] = restConfig.KeyData
    62  	}
    63  
    64  	secretYamlData, err := yaml.Marshal(secret)
    65  	if err != nil {
    66  		klog.Fatal(err)
    67  	}
    68  
    69  	fmt.Print(string(secretYamlData))
    70  }