...

Source file src/edge-infra.dev/pkg/edge/bootstrapping/connection_secret.go

Documentation: edge-infra.dev/pkg/edge/bootstrapping

     1  package bootstrapping
     2  
     3  import (
     4  	corev1 "k8s.io/api/core/v1"
     5  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     6  )
     7  
     8  const (
     9  	regionConnectionSecretName = "region-connection"
    10  	regionObjectNamespace      = "edge-agent"
    11  	secretKind                 = "Secret"
    12  	secretAPIVersion           = "v1"
    13  	defaultStr                 = ""
    14  )
    15  
    16  type ConnectionSecret struct {
    17  	typeMetaKind        string
    18  	typeMetaAPIVersion  string
    19  	objectMetaName      string
    20  	objectMetaNamespace string
    21  	clusterCA           string
    22  	endpoint            string
    23  	namespace           string
    24  	projectID           string
    25  	// TODO: convert this to clusterEdgeID or is it deprecated?
    26  	// clusterID           string
    27  	clusterType    string
    28  	storeNamespace string
    29  	joined         string
    30  }
    31  
    32  func CreateRegionConnectionSecret() *ConnectionSecret {
    33  	return &ConnectionSecret{
    34  		typeMetaKind:        secretKind,
    35  		typeMetaAPIVersion:  secretAPIVersion,
    36  		objectMetaName:      regionConnectionSecretName,
    37  		objectMetaNamespace: regionObjectNamespace,
    38  		namespace:           defaultStr,
    39  		projectID:           defaultStr,
    40  		// clusterID:           defaultStr,
    41  		clusterType:    defaultStr,
    42  		storeNamespace: defaultStr,
    43  		joined:         defaultStr,
    44  	}
    45  }
    46  
    47  func (c *ConnectionSecret) TypeMetaKind(kind string) *ConnectionSecret {
    48  	c.typeMetaKind = kind
    49  	return c
    50  }
    51  
    52  func (c *ConnectionSecret) TypeMetaAPIVersion(apiVersion string) *ConnectionSecret {
    53  	c.typeMetaAPIVersion = apiVersion
    54  	return c
    55  }
    56  
    57  func (c *ConnectionSecret) ObjectMetaName(name string) *ConnectionSecret {
    58  	c.objectMetaName = name
    59  	return c
    60  }
    61  
    62  func (c *ConnectionSecret) ObjectMetaNamespace(ns string) *ConnectionSecret {
    63  	c.objectMetaNamespace = ns
    64  	return c
    65  }
    66  
    67  func (c *ConnectionSecret) ClusterCA(ca string) *ConnectionSecret {
    68  	c.clusterCA = ca
    69  	return c
    70  }
    71  
    72  func (c *ConnectionSecret) Endpoint(endpoint string) *ConnectionSecret {
    73  	c.endpoint = endpoint
    74  	return c
    75  }
    76  
    77  func (c *ConnectionSecret) Namespace(ns string) *ConnectionSecret {
    78  	c.namespace = ns
    79  	return c
    80  }
    81  
    82  func (c *ConnectionSecret) ProjectID(pid string) *ConnectionSecret {
    83  	c.projectID = pid
    84  	return c
    85  }
    86  
    87  // func (c *ConnectionSecret) ClusterID(cid string) *ConnectionSecret {
    88  // 	c.clusterID = cid
    89  // 	return c
    90  // }
    91  
    92  func (c *ConnectionSecret) ClusterType(ct string) *ConnectionSecret {
    93  	c.clusterType = ct
    94  	return c
    95  }
    96  
    97  func (c *ConnectionSecret) StoreNamespace(sns string) *ConnectionSecret {
    98  	c.storeNamespace = sns
    99  	return c
   100  }
   101  
   102  func (c *ConnectionSecret) Joined(joined string) *ConnectionSecret {
   103  	c.joined = joined
   104  	return c
   105  }
   106  
   107  func (c *ConnectionSecret) Build(stringData bool) *corev1.Secret {
   108  	connectionSecret := &corev1.Secret{
   109  		TypeMeta: metav1.TypeMeta{
   110  			Kind:       c.typeMetaKind,
   111  			APIVersion: c.typeMetaAPIVersion,
   112  		},
   113  		ObjectMeta: metav1.ObjectMeta{
   114  			Name:      c.objectMetaName,
   115  			Namespace: c.objectMetaNamespace,
   116  		},
   117  	}
   118  
   119  	data := removeEmptyFields(map[string]string{
   120  		"clusterCA": c.clusterCA,
   121  		"endpoint":  c.endpoint,
   122  		"namespace": c.namespace,
   123  		"projectID": c.projectID,
   124  		// "clusterID":      c.clusterID,
   125  		"clusterType":    c.clusterType,
   126  		"storeNamespace": c.storeNamespace,
   127  		"joined":         c.joined,
   128  	})
   129  
   130  	if stringData {
   131  		connectionSecret.StringData = removeEmptyFields(data)
   132  	} else {
   133  		byteData := make(map[string][]byte)
   134  		for key, val := range data {
   135  			byteData[key] = []byte(val)
   136  		}
   137  		connectionSecret.Data = byteData
   138  	}
   139  	return connectionSecret
   140  }
   141  
   142  func removeEmptyFields(data map[string]string) map[string]string {
   143  	for key, val := range data {
   144  		if val == defaultStr {
   145  			delete(data, key)
   146  		}
   147  	}
   148  	return data
   149  }
   150  

View as plain text