github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/internal/k8s/kubeconfig.go (about)

     1  // Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package k8s
     5  
     6  // KubeConfig represents a kubeconfig object used to connect using a token
     7  type KubeConfig struct {
     8  	Clusters       []kcCluster `json:"clusters"`
     9  	Users          []kcUser    `json:"users"`
    10  	Contexts       []kcContext `json:"contexts"`
    11  	CurrentContext string      `json:"current-context"`
    12  }
    13  type kcCluster struct {
    14  	Name    string        `json:"name"`
    15  	Cluster kcClusterData `json:"cluster"`
    16  }
    17  type kcClusterData struct {
    18  	Server   string `json:"server"`
    19  	CertAuth string `json:"certificate-authority-data"`
    20  }
    21  type kcUser struct {
    22  	Name string     `json:"name"`
    23  	User kcUserData `json:"user"`
    24  }
    25  type kcUserData struct {
    26  	Token string `json:"token"`
    27  }
    28  type kcContext struct {
    29  	Name    string        `json:"name"`
    30  	Context kcContextData `json:"context"`
    31  }
    32  type kcContextData struct {
    33  	User    string `json:"user"`
    34  	Cluster string `json:"cluster"`
    35  }
    36  
    37  // KubeconfigBuilder contains fields needed to build a KubeConfig object
    38  type KubeconfigBuilder struct {
    39  	ClusterName string
    40  	Server      string
    41  	CertAuth    string
    42  	UserName    string
    43  	UserToken   string
    44  	ContextName string
    45  }
    46  
    47  // Build creates a KubeConfig object using the fields of the builder
    48  func (b *KubeconfigBuilder) Build() KubeConfig {
    49  	return KubeConfig{
    50  		Clusters: []kcCluster{{
    51  			Name: b.ClusterName,
    52  			Cluster: kcClusterData{
    53  				Server:   b.Server,
    54  				CertAuth: b.CertAuth,
    55  			},
    56  		}},
    57  		Users: []kcUser{{
    58  			Name: b.UserName,
    59  			User: kcUserData{
    60  				Token: b.UserToken,
    61  			},
    62  		}},
    63  		Contexts: []kcContext{{
    64  			Name: b.ContextName,
    65  			Context: kcContextData{
    66  				User:    b.UserName,
    67  				Cluster: b.ClusterName,
    68  			},
    69  		}},
    70  		CurrentContext: b.ContextName,
    71  	}
    72  }