github.com/openshift/installer@v1.4.17/pkg/asset/manifests/nutanix/cloudproviderconfig.go (about)

     1  package nutanix
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	nutanixtypes "github.com/openshift/installer/pkg/types/nutanix"
     9  )
    10  
    11  // CloudConfig is the config of Nutanix cloud provider
    12  // ref: https://github.com/nutanix-cloud-native/cloud-provider-nutanix/blob/main/pkg/provider/config/config.go
    13  type CloudConfig struct {
    14  	PrismCentral         PrismEndpoint     `json:"prismCentral"`
    15  	TopologyDiscovery    TopologyDiscovery `json:"topologyDiscovery"`
    16  	EnableCustomLabeling bool              `json:"enableCustomLabeling"`
    17  }
    18  
    19  // TopologyDiscovery of the cloud provider.
    20  type TopologyDiscovery struct {
    21  	// Default type will be set to Prism via the newConfig function
    22  	Type               TopologyDiscoveryType `json:"type"`
    23  	TopologyCategories *TopologyCategories   `json:"topologyCategories"`
    24  }
    25  
    26  // TopologyDiscoveryType type alias.
    27  type TopologyDiscoveryType string
    28  
    29  const (
    30  	// PrismTopologyDiscoveryType is the DiscoveryType for Prism provider.
    31  	PrismTopologyDiscoveryType = TopologyDiscoveryType("Prism")
    32  	// CategoriesTopologyDiscoveryType is the DiscoveryType for Categories provider.
    33  	CategoriesTopologyDiscoveryType = TopologyDiscoveryType("Categories")
    34  )
    35  
    36  // TopologyInfo contains topology information.
    37  type TopologyInfo struct {
    38  	Zone   string `json:"zone"`
    39  	Region string `json:"region"`
    40  }
    41  
    42  // TopologyCategories contains topology categories.
    43  type TopologyCategories struct {
    44  	ZoneCategory   string `json:"zoneCategory"`
    45  	RegionCategory string `json:"regionCategory"`
    46  }
    47  
    48  // PrismEndpoint contains endpoint details for Prism provider.
    49  type PrismEndpoint struct {
    50  	// address is the endpoint address (DNS name or IP address) of the Nutanix Prism Central or Element (cluster)
    51  	Address string `json:"address"`
    52  
    53  	// port is the port number to access the Nutanix Prism Central or Element (cluster)
    54  	Port int32 `json:"port"`
    55  
    56  	// Pass credential information for the target Prism instance
    57  	// +optional
    58  	CredentialRef *CredentialReference `json:"credentialRef,omitempty"`
    59  }
    60  
    61  // CredentialKind type alias.
    62  type CredentialKind string
    63  
    64  // SecretKind a credential of type "Secret".
    65  var SecretKind = CredentialKind("Secret")
    66  
    67  // CredentialReference holds details of a credential.
    68  type CredentialReference struct {
    69  	// Kind of the Nutanix credential
    70  	Kind CredentialKind `json:"kind"`
    71  
    72  	// Name of the credential.
    73  	Name string `json:"name"`
    74  	// namespace of the credential.
    75  	Namespace string `json:"namespace"`
    76  }
    77  
    78  // CloudConfigJSON returns the json string of the created CloudConfig
    79  // based on the input nutanix platform config.
    80  func CloudConfigJSON(nutanixPlatform *nutanixtypes.Platform) (string, error) {
    81  	nutanixCloudConfig := CloudConfig{
    82  		PrismCentral: PrismEndpoint{
    83  			Address: nutanixPlatform.PrismCentral.Endpoint.Address,
    84  			Port:    nutanixPlatform.PrismCentral.Endpoint.Port,
    85  			CredentialRef: &CredentialReference{
    86  				Kind:      "Secret",
    87  				Name:      "nutanix-credentials",
    88  				Namespace: "openshift-cloud-controller-manager",
    89  			},
    90  		},
    91  		TopologyDiscovery: TopologyDiscovery{
    92  			Type: PrismTopologyDiscoveryType,
    93  		},
    94  		EnableCustomLabeling: true,
    95  	}
    96  	configData, err := nutanixCloudConfig.JSON()
    97  	if err != nil {
    98  		return "", fmt.Errorf("could not create Nutanix Cloud provider config. %w", err)
    99  	}
   100  
   101  	return configData, nil
   102  }
   103  
   104  // JSON generates the cloud provider json config for the Nutanix platform.
   105  func (config CloudConfig) JSON() (string, error) {
   106  	buff := &bytes.Buffer{}
   107  	encoder := json.NewEncoder(buff)
   108  	encoder.SetIndent("", "\t")
   109  	if err := encoder.Encode(config); err != nil {
   110  		return "", err
   111  	}
   112  
   113  	return buff.String(), nil
   114  }