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

     1  package manifests
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/pkg/errors"
    10  	corev1 "k8s.io/api/core/v1"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"sigs.k8s.io/yaml"
    13  
    14  	hiveext "github.com/openshift/assisted-service/api/hiveextension/v1beta1"
    15  	hivev1 "github.com/openshift/hive/apis/hive/v1"
    16  	"github.com/openshift/installer/pkg/asset"
    17  	"github.com/openshift/installer/pkg/asset/agent"
    18  	"github.com/openshift/installer/pkg/asset/agent/workflow"
    19  )
    20  
    21  var (
    22  	clusterDeploymentFilename = filepath.Join(clusterManifestDir, "cluster-deployment.yaml")
    23  )
    24  
    25  // ClusterDeployment generates the cluster-deployment.yaml file.
    26  type ClusterDeployment struct {
    27  	File   *asset.File
    28  	Config *hivev1.ClusterDeployment
    29  }
    30  
    31  var _ asset.WritableAsset = (*ClusterDeployment)(nil)
    32  
    33  // Name returns a human friendly name for the asset.
    34  func (*ClusterDeployment) Name() string {
    35  	return "ClusterDeployment Config"
    36  }
    37  
    38  // Dependencies returns all of the dependencies directly needed to generate
    39  // the asset.
    40  func (*ClusterDeployment) Dependencies() []asset.Asset {
    41  	return []asset.Asset{
    42  		&workflow.AgentWorkflow{},
    43  		&agent.OptionalInstallConfig{},
    44  	}
    45  }
    46  
    47  // Generate generates the ClusterDeployment manifest.
    48  func (cd *ClusterDeployment) Generate(_ context.Context, dependencies asset.Parents) error {
    49  	agentWorkflow := &workflow.AgentWorkflow{}
    50  	installConfig := &agent.OptionalInstallConfig{}
    51  	dependencies.Get(agentWorkflow, installConfig)
    52  
    53  	// This manifest is not required for AddNodes workflow
    54  	if agentWorkflow.Workflow == workflow.AgentWorkflowTypeAddNodes {
    55  		return nil
    56  	}
    57  
    58  	if installConfig.Config != nil {
    59  		clusterDeployment := &hivev1.ClusterDeployment{
    60  			TypeMeta: metav1.TypeMeta{
    61  				Kind:       "ClusterDeployment",
    62  				APIVersion: hivev1.SchemeGroupVersion.String(),
    63  			},
    64  			ObjectMeta: metav1.ObjectMeta{
    65  				Name:      getClusterDeploymentName(installConfig),
    66  				Namespace: installConfig.ClusterNamespace(),
    67  			},
    68  			Spec: hivev1.ClusterDeploymentSpec{
    69  				ClusterName: getClusterDeploymentName(installConfig),
    70  				BaseDomain:  installConfig.Config.BaseDomain,
    71  				PullSecretRef: &corev1.LocalObjectReference{
    72  					Name: getPullSecretName(installConfig.ClusterName()),
    73  				},
    74  				ClusterInstallRef: &hivev1.ClusterInstallLocalReference{
    75  					Group:   hiveext.Group,
    76  					Version: hiveext.Version,
    77  					Kind:    "AgentClusterInstall",
    78  					Name:    getAgentClusterInstallName(installConfig),
    79  				},
    80  			},
    81  		}
    82  
    83  		cd.Config = clusterDeployment
    84  		clusterDeploymentData, err := yaml.Marshal(clusterDeployment)
    85  		if err != nil {
    86  			return errors.Wrap(err, "failed to marshal agent installer ClusterDeployment")
    87  		}
    88  
    89  		cd.File = &asset.File{
    90  			Filename: clusterDeploymentFilename,
    91  			Data:     clusterDeploymentData,
    92  		}
    93  
    94  	}
    95  
    96  	return cd.finish()
    97  }
    98  
    99  // Files returns the files generated by the asset.
   100  func (cd *ClusterDeployment) Files() []*asset.File {
   101  	if cd.File != nil {
   102  		return []*asset.File{cd.File}
   103  	}
   104  	return []*asset.File{}
   105  }
   106  
   107  // Load returns ClusterDeployment asset from the disk.
   108  func (cd *ClusterDeployment) Load(f asset.FileFetcher) (bool, error) {
   109  
   110  	file, err := f.FetchByName(clusterDeploymentFilename)
   111  	if err != nil {
   112  		if os.IsNotExist(err) {
   113  			return false, nil
   114  		}
   115  		return false, errors.Wrap(err, fmt.Sprintf("failed to load %s file", clusterDeploymentFilename))
   116  	}
   117  
   118  	config := &hivev1.ClusterDeployment{}
   119  	if err := yaml.UnmarshalStrict(file.Data, config); err != nil {
   120  		return false, errors.Wrapf(err, "failed to unmarshal %s", clusterDeploymentFilename)
   121  	}
   122  
   123  	cd.File, cd.Config = file, config
   124  	if err = cd.finish(); err != nil {
   125  		return false, err
   126  	}
   127  
   128  	return true, nil
   129  }
   130  
   131  func (cd *ClusterDeployment) finish() error {
   132  
   133  	if cd.Config == nil {
   134  		return errors.New("missing configuration or manifest file")
   135  	}
   136  
   137  	return nil
   138  }