k8c.io/api/v3@v3.0.0-20230904060738-b0a93889c0b6/pkg/apis/kubermatic/v1/helper/machine_controller.go (about)

     1  /*
     2  Copyright 2023 The Kubermatic Kubernetes Platform contributors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package helper
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	kubermaticv1 "k8c.io/api/v3/pkg/apis/kubermatic/v1"
    24  	machinecontroller "k8c.io/api/v3/pkg/apis/machine-controller"
    25  )
    26  
    27  var (
    28  	cloudProviderMap = map[kubermaticv1.CloudProvider]machinecontroller.CloudProvider{
    29  		kubermaticv1.CloudProviderFake:                machinecontroller.CloudProviderFake,
    30  		kubermaticv1.CloudProviderAlibaba:             machinecontroller.CloudProviderAlibaba,
    31  		kubermaticv1.CloudProviderAnexia:              machinecontroller.CloudProviderAnexia,
    32  		kubermaticv1.CloudProviderAWS:                 machinecontroller.CloudProviderAWS,
    33  		kubermaticv1.CloudProviderAzure:               machinecontroller.CloudProviderAzure,
    34  		kubermaticv1.CloudProviderBringYourOwn:        machinecontroller.CloudProvider(""),
    35  		kubermaticv1.CloudProviderDigitalocean:        machinecontroller.CloudProviderDigitalocean,
    36  		kubermaticv1.CloudProviderGCP:                 machinecontroller.CloudProviderGoogle,
    37  		kubermaticv1.CloudProviderHetzner:             machinecontroller.CloudProviderHetzner,
    38  		kubermaticv1.CloudProviderKubeVirt:            machinecontroller.CloudProviderKubeVirt,
    39  		kubermaticv1.CloudProviderNutanix:             machinecontroller.CloudProviderNutanix,
    40  		kubermaticv1.CloudProviderOpenStack:           machinecontroller.CloudProviderOpenStack,
    41  		kubermaticv1.CloudProviderPacket:              machinecontroller.CloudProviderPacket,
    42  		kubermaticv1.CloudProviderVMwareCloudDirector: machinecontroller.CloudProviderVMwareCloudDirector,
    43  		kubermaticv1.CloudProviderVSphere:             machinecontroller.CloudProviderVSphere,
    44  	}
    45  )
    46  
    47  func CloudProviderToMachineController(provider kubermaticv1.CloudProvider) (machinecontroller.CloudProvider, error) {
    48  	mapped, exists := cloudProviderMap[provider]
    49  	if !exists {
    50  		return machinecontroller.CloudProvider(""), fmt.Errorf("unknown cloud provider %q", provider)
    51  	}
    52  
    53  	if mapped == "" {
    54  		return machinecontroller.CloudProvider(""), fmt.Errorf("KKP provider %q has no equivalent in the machine-controller", provider)
    55  	}
    56  
    57  	return mapped, nil
    58  }
    59  
    60  func CloudProviderToKKP(provider machinecontroller.CloudProvider) (kubermaticv1.CloudProvider, error) {
    61  	// make sure non-existing mappings in one direction (kkp=>mc) do not accidentally create
    62  	// a fake mapping when going the other direction (mc=>kkp)
    63  	if provider == "" {
    64  		return kubermaticv1.CloudProvider(""), errors.New("no cloud provider given")
    65  	}
    66  
    67  	for kkp, mc := range cloudProviderMap {
    68  		if provider == mc {
    69  			return kkp, nil
    70  		}
    71  	}
    72  
    73  	return kubermaticv1.CloudProvider(""), fmt.Errorf("unknown cloud provider %q", provider)
    74  }
    75  
    76  var (
    77  	operatingSystemMap = map[kubermaticv1.OperatingSystem]machinecontroller.OperatingSystem{
    78  		kubermaticv1.OperatingSystemUbuntu:       machinecontroller.OperatingSystemUbuntu,
    79  		kubermaticv1.OperatingSystemCentOS:       machinecontroller.OperatingSystemCentOS,
    80  		kubermaticv1.OperatingSystemAmazonLinux2: machinecontroller.OperatingSystemAmazonLinux2,
    81  		kubermaticv1.OperatingSystemRHEL:         machinecontroller.OperatingSystemRHEL,
    82  		kubermaticv1.OperatingSystemFlatcar:      machinecontroller.OperatingSystemFlatcar,
    83  		kubermaticv1.OperatingSystemRockyLinux:   machinecontroller.OperatingSystemRockyLinux,
    84  	}
    85  )
    86  
    87  func OperatingSystemToMachineController(os kubermaticv1.OperatingSystem) (machinecontroller.OperatingSystem, error) {
    88  	mapped, exists := operatingSystemMap[os]
    89  	if !exists {
    90  		return machinecontroller.OperatingSystem(""), fmt.Errorf("unknown operating system %q", os)
    91  	}
    92  
    93  	if mapped == "" {
    94  		return machinecontroller.OperatingSystem(""), fmt.Errorf("KKP cloud provider %q has no equivalent in the machine-controller", os)
    95  	}
    96  
    97  	return mapped, nil
    98  }
    99  
   100  func OperatingSystemToKKP(os machinecontroller.OperatingSystem) (kubermaticv1.OperatingSystem, error) {
   101  	// make sure non-existing mappings in one direction (kkp=>mc) do not accidentally create
   102  	// a fake mapping when going the other direction (mc=>kkp)
   103  	if os == "" {
   104  		return kubermaticv1.OperatingSystem(""), errors.New("no operating system given")
   105  	}
   106  
   107  	for kkp, mc := range operatingSystemMap {
   108  		if os == mc {
   109  			return kkp, nil
   110  		}
   111  	}
   112  
   113  	return kubermaticv1.OperatingSystem(""), fmt.Errorf("unknown operating system %q", os)
   114  }