github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/k8s/k8sgen.go (about)

     1  /*
     2  Copyright 2019 The OpenEBS Authors
     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 k8s
    18  
    19  import (
    20  	"github.com/openebs/node-disk-manager/integration_tests/utils"
    21  	appsv1 "k8s.io/api/apps/v1"
    22  	v1 "k8s.io/api/core/v1"
    23  	rbacv1 "k8s.io/api/rbac/v1"
    24  	apiextensionsV1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  	"sigs.k8s.io/yaml"
    26  )
    27  
    28  // NDMYaml is a string type that stores the path to the YAML files
    29  // which are required to deploy NDM
    30  type NDMYaml string
    31  
    32  // Path to various YAMLs used for integration testing
    33  const (
    34  	ConfigMapYAML           NDMYaml = "../yamls/node-disk-manager-config.yaml"
    35  	ServiceAccountYAML      NDMYaml = "../../deploy/yamls/serviceaccount.yaml"
    36  	ClusterRoleYAML         NDMYaml = "../../deploy/yamls/clusterrole.yaml"
    37  	ClusterRoleBindingYAML  NDMYaml = "../../deploy/yamls/clusterrolebinding.yaml"
    38  	BlockDeviceCRDYAML      NDMYaml = "../../deploy/crds/openebs.io_blockdevices.yaml"
    39  	BlockDeviceClaimCRDYAML NDMYaml = "../../deploy/crds/openebs.io_blockdeviceclaims.yaml"
    40  	DaemonSetYAML           NDMYaml = "../yamls/node-disk-manager.yaml"
    41  	DeploymentYAML          NDMYaml = "../../deploy/yamls/node-disk-operator.yaml"
    42  	OpenEBSNamespaceYAML    NDMYaml = "../../deploy/yamls/namespace.yaml"
    43  )
    44  
    45  // GetNamespace generates the openebs namespace object from yaml file
    46  func GetNamespace() (v1.Namespace, error) {
    47  	var ns v1.Namespace
    48  	yamlstring, err := utils.GetYAMLString(string(OpenEBSNamespaceYAML))
    49  	if err != nil {
    50  		return ns, err
    51  	}
    52  	err = yaml.Unmarshal([]byte(yamlstring), &ns)
    53  	return ns, err
    54  }
    55  
    56  // GetConfigMap generates the ConfigMap object for NDM from the yaml file
    57  func GetConfigMap() (v1.ConfigMap, error) {
    58  	var configMap v1.ConfigMap
    59  	yamlstring, err := utils.GetYAMLString(string(ConfigMapYAML))
    60  	if err != nil {
    61  		return configMap, err
    62  	}
    63  	err = yaml.Unmarshal([]byte(yamlstring), &configMap)
    64  	return configMap, err
    65  }
    66  
    67  // GetServiceAccount generates the ServiceAccount object from the yaml file
    68  func GetServiceAccount() (v1.ServiceAccount, error) {
    69  	var serviceAccount v1.ServiceAccount
    70  	yamlstring, err := utils.GetYAMLString(string(ServiceAccountYAML))
    71  	if err != nil {
    72  		return serviceAccount, err
    73  	}
    74  	err = yaml.Unmarshal([]byte(yamlstring), &serviceAccount)
    75  	return serviceAccount, err
    76  }
    77  
    78  // GetClusterRole generates the ClusterRole object from the yaml file
    79  func GetClusterRole() (rbacv1.ClusterRole, error) {
    80  	var clusterRole rbacv1.ClusterRole
    81  	yamlstring, err := utils.GetYAMLString(string(ClusterRoleYAML))
    82  	if err != nil {
    83  		return clusterRole, err
    84  	}
    85  	err = yaml.Unmarshal([]byte(yamlstring), &clusterRole)
    86  	return clusterRole, err
    87  }
    88  
    89  // GetClusterRoleBinding generates the ClusterRoleBinding object from the yaml file
    90  func GetClusterRoleBinding() (rbacv1.ClusterRoleBinding, error) {
    91  	var clusterRoleBinding rbacv1.ClusterRoleBinding
    92  	yamlstring, err := utils.GetYAMLString(string(ClusterRoleBindingYAML))
    93  	if err != nil {
    94  		return clusterRoleBinding, err
    95  	}
    96  	err = yaml.Unmarshal([]byte(yamlstring), &clusterRoleBinding)
    97  	return clusterRoleBinding, err
    98  }
    99  
   100  // GetCustomResourceDefinition generates the CustomResourceDefinition object from the specified
   101  // YAML file
   102  func GetCustomResourceDefinition(crdyaml NDMYaml) (apiextensionsV1.CustomResourceDefinition, error) {
   103  	var customResourceDefinition apiextensionsV1.CustomResourceDefinition
   104  	yamlString, err := utils.GetYAMLString(string(crdyaml))
   105  	if err != nil {
   106  		return customResourceDefinition, err
   107  	}
   108  	err = yaml.Unmarshal([]byte(yamlString), &customResourceDefinition)
   109  	return customResourceDefinition, err
   110  }
   111  
   112  // GetDaemonSet generates the NDM DaemonSet object from the yaml file
   113  func GetDaemonSet() (appsv1.DaemonSet, error) {
   114  	var daemonSet appsv1.DaemonSet
   115  	yamlstring, err := utils.GetYAMLString(string(DaemonSetYAML))
   116  	if err != nil {
   117  		return daemonSet, err
   118  	}
   119  	err = yaml.Unmarshal([]byte(yamlstring), &daemonSet)
   120  	return daemonSet, err
   121  }
   122  
   123  // GetDeployment generates the NDO Deployment object from the yaml file
   124  func GetDeployment() (appsv1.Deployment, error) {
   125  	var deployment appsv1.Deployment
   126  	yamlstring, err := utils.GetYAMLString(string(DeploymentYAML))
   127  	if err != nil {
   128  		return deployment, err
   129  	}
   130  	err = yaml.Unmarshal([]byte(yamlstring), &deployment)
   131  	return deployment, err
   132  }