...

Source file src/edge-infra.dev/pkg/edge/info/config.go

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

     1  package info
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	v1 "k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"sigs.k8s.io/controller-runtime/pkg/client"
    12  
    13  	"edge-infra.dev/pkg/edge/constants/api/fleet"
    14  )
    15  
    16  type EdgeInfo struct {
    17  	BannerName       string
    18  	ProjectID        string
    19  	Store            string
    20  	Fleet            string
    21  	ClusterType      string
    22  	Location         string
    23  	ForemanProjectID string
    24  	BannerEdgeID     string
    25  	ClusterEdgeID    string
    26  	EdgeAPIEndpoint  string
    27  }
    28  
    29  // FromConfigMap retrieves data from ConfigMap to EdgeInfo
    30  func (i *EdgeInfo) FromConfigMap(cfg *v1.ConfigMap) *EdgeInfo {
    31  	i.BannerName = cfg.Data[Banner]
    32  	i.ProjectID = cfg.Data[ProjectID]
    33  	i.Store = cfg.Data[StoreName]
    34  	i.ClusterEdgeID = cfg.Data[ClusterEdgeID]
    35  	i.ClusterType = cfg.Data[ClusterType]
    36  	i.Fleet = cfg.Data[FleetType]
    37  	i.Location = cfg.Data[K8sClusterLocation]
    38  	i.ForemanProjectID = cfg.Data[ForemanProjectID]
    39  	i.BannerEdgeID = cfg.Data[BannerID]
    40  	i.EdgeAPIEndpoint = cfg.Data[EdgeAPIEndpoint]
    41  	return i
    42  }
    43  
    44  // ToConfigMap create edge-info ConfigMap
    45  func (i EdgeInfo) ToConfigMap() *v1.ConfigMap {
    46  	return &v1.ConfigMap{
    47  		TypeMeta: metav1.TypeMeta{
    48  			Kind:       "ConfigMap",
    49  			APIVersion: v1.SchemeGroupVersion.String(),
    50  		},
    51  		ObjectMeta: metav1.ObjectMeta{
    52  			Name:      EdgeConfigMapName,
    53  			Namespace: EdgeConfigMapNS,
    54  		},
    55  		Data: map[string]string{
    56  			Banner:             i.BannerName,
    57  			ProjectID:          i.ProjectID,
    58  			StoreName:          i.Store,
    59  			ClusterEdgeID:      i.ClusterEdgeID,
    60  			ClusterType:        i.ClusterType,
    61  			K8sClusterLocation: i.Location,
    62  			FleetType:          i.Fleet,
    63  			ForemanProjectID:   i.ForemanProjectID,
    64  			BannerID:           i.BannerEdgeID,
    65  			EdgeAPIEndpoint:    i.EdgeAPIEndpoint,
    66  		},
    67  	}
    68  }
    69  
    70  // ValidateConfigMap validates edge-info ConfigMap
    71  func ValidateConfigMap(cfg *v1.ConfigMap) error {
    72  	f := cfg.Data[FleetType]
    73  	notTopLevelCluster := f != fleet.Banner && f != fleet.TopLevel
    74  
    75  	var missing []string
    76  	if notTopLevelCluster && cfg.Data[Banner] == "" {
    77  		missing = append(missing, Banner)
    78  	}
    79  	if cfg.Data[ProjectID] == "" {
    80  		missing = append(missing, ProjectID)
    81  	}
    82  	if cfg.Data[StoreName] == "" {
    83  		missing = append(missing, StoreName)
    84  	}
    85  	if cfg.Data[ClusterEdgeID] == "" {
    86  		missing = append(missing, ClusterEdgeID)
    87  	}
    88  	if cfg.Data[ClusterType] == "" {
    89  		missing = append(missing, ClusterType)
    90  	}
    91  	if notTopLevelCluster && cfg.Data[K8sClusterLocation] == "" {
    92  		missing = append(missing, K8sClusterLocation)
    93  	}
    94  	if len(missing) > 0 {
    95  		// TODO add name namespace
    96  		return fmt.Errorf("edge-info configmap invalid, value(s) not provided: %s", strings.Join(missing, ","))
    97  	}
    98  	return nil
    99  }
   100  
   101  // New creates a valid EdgeInfo ref
   102  func New(cfg *v1.ConfigMap) (*EdgeInfo, error) {
   103  	if err := ValidateConfigMap(cfg); err != nil {
   104  		return nil, err
   105  	}
   106  	return FromConfigMap(cfg), nil
   107  }
   108  
   109  // FromConfigMap util function to create EdgeInfo from config map
   110  func FromConfigMap(cfg *v1.ConfigMap) *EdgeInfo {
   111  	return (&EdgeInfo{}).FromConfigMap(cfg)
   112  }
   113  
   114  // IsEdgeInfoConfigMap util function to check if object is edge info config map
   115  func IsEdgeInfoConfigMap(name, namespace string) bool {
   116  	hasRequiredName := name == EdgeConfigMapName
   117  	inRequiredNamespace := namespace == EdgeConfigMapNS
   118  	return hasRequiredName && inRequiredNamespace
   119  }
   120  
   121  // FromClient grabs the configmap from the cluster
   122  func FromClient(ctx context.Context, cl client.Client) (*EdgeInfo, error) {
   123  	cfg := &v1.ConfigMap{}
   124  	key := client.ObjectKey{Namespace: EdgeConfigMapNS, Name: EdgeConfigMapName}
   125  	if err := cl.Get(ctx, key, cfg); err != nil {
   126  		return nil, err
   127  	}
   128  	return New(cfg)
   129  }
   130  
   131  // BuildConfigMap creates an edge-info configmap
   132  func BuildConfigMap(projectID, store, storeType, location, banner, fleet, foremanProjectID, bannerID, clusterEdgeID, edgeAPIEndpoint string) *v1.ConfigMap {
   133  	i := &EdgeInfo{
   134  		BannerName:       banner,
   135  		ProjectID:        projectID,
   136  		ClusterEdgeID:    clusterEdgeID,
   137  		Store:            store,
   138  		ClusterType:      storeType,
   139  		Location:         location,
   140  		Fleet:            fleet,
   141  		ForemanProjectID: foremanProjectID,
   142  		BannerEdgeID:     bannerID,
   143  		EdgeAPIEndpoint:  edgeAPIEndpoint,
   144  	}
   145  	return i.ToConfigMap()
   146  }
   147  
   148  // ConfigMapToString converts the provided configmap to a string.
   149  func ConfigMapToString(configMap *v1.ConfigMap) ([]byte, error) {
   150  	return json.Marshal(configMap)
   151  }
   152  

View as plain text