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

     1  package manifests
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  
     8  	"github.com/pkg/errors"
     9  	corev1 "k8s.io/api/core/v1"
    10  	"k8s.io/apimachinery/pkg/util/validation/field"
    11  
    12  	hiveext "github.com/openshift/assisted-service/api/hiveextension/v1beta1"
    13  	aiv1beta1 "github.com/openshift/assisted-service/api/v1beta1"
    14  	"github.com/openshift/assisted-service/models"
    15  	hivev1 "github.com/openshift/hive/apis/hive/v1"
    16  	"github.com/openshift/installer/pkg/asset"
    17  )
    18  
    19  const (
    20  	// This could be change to "cluster-manifests" once all the agent code will be migrated to using
    21  	// assets (and will stop reading from the hard-code "manifests" relative path)
    22  	clusterManifestDir = "cluster-manifests"
    23  )
    24  
    25  var (
    26  	_ asset.WritableAsset = (*AgentManifests)(nil)
    27  )
    28  
    29  // AgentManifests generates all the required manifests by the agent installer.
    30  type AgentManifests struct {
    31  	FileList []*asset.File
    32  
    33  	PullSecret           *corev1.Secret
    34  	InfraEnv             *aiv1beta1.InfraEnv
    35  	StaticNetworkConfigs []*models.HostStaticNetworkConfig
    36  	NMStateConfigs       []*aiv1beta1.NMStateConfig
    37  	AgentClusterInstall  *hiveext.AgentClusterInstall
    38  	ClusterDeployment    *hivev1.ClusterDeployment
    39  	ClusterImageSet      *hivev1.ClusterImageSet
    40  }
    41  
    42  // Name returns a human friendly name.
    43  func (m *AgentManifests) Name() string {
    44  	return "Agent Manifests"
    45  }
    46  
    47  // Dependencies returns all of the dependencies directly needed the asset.
    48  func (m *AgentManifests) Dependencies() []asset.Asset {
    49  	return []asset.Asset{
    50  		&AgentPullSecret{},
    51  		&InfraEnv{},
    52  		&NMStateConfig{},
    53  		&AgentClusterInstall{},
    54  		&ClusterDeployment{},
    55  		&ClusterImageSet{},
    56  	}
    57  }
    58  
    59  // Generate generates the respective manifest files.
    60  func (m *AgentManifests) Generate(_ context.Context, dependencies asset.Parents) error {
    61  	for _, a := range []asset.WritableAsset{
    62  		&AgentPullSecret{},
    63  		&InfraEnv{},
    64  		&NMStateConfig{},
    65  		&AgentClusterInstall{},
    66  		&ClusterDeployment{},
    67  		&ClusterImageSet{},
    68  	} {
    69  		dependencies.Get(a)
    70  
    71  		switch v := a.(type) {
    72  		case *AgentPullSecret:
    73  			m.PullSecret = v.Config
    74  		case *InfraEnv:
    75  			m.InfraEnv = v.Config
    76  		case *NMStateConfig:
    77  			// continue if there are no configs defined to avoid
    78  			// writing out a empty nmstateconfig.yaml file
    79  			if len(v.Config) == 0 {
    80  				continue
    81  			}
    82  			m.StaticNetworkConfigs = append(m.StaticNetworkConfigs, v.StaticNetworkConfig...)
    83  			m.NMStateConfigs = append(m.NMStateConfigs, v.Config...)
    84  		case *AgentClusterInstall:
    85  			m.AgentClusterInstall = v.Config
    86  		case *ClusterDeployment:
    87  			m.ClusterDeployment = v.Config
    88  		case *ClusterImageSet:
    89  			m.ClusterImageSet = v.Config
    90  		}
    91  
    92  		m.FileList = append(m.FileList, a.Files()...)
    93  	}
    94  
    95  	asset.SortFiles(m.FileList)
    96  
    97  	return m.finish()
    98  }
    99  
   100  // Files returns the files generated by the asset.
   101  func (m *AgentManifests) Files() []*asset.File {
   102  	return m.FileList
   103  }
   104  
   105  // Load currently does nothing
   106  func (m *AgentManifests) Load(f asset.FileFetcher) (bool, error) {
   107  	return false, nil
   108  }
   109  
   110  // GetPullSecretData returns the content of the pull secret
   111  func (m *AgentManifests) GetPullSecretData() string {
   112  	return m.PullSecret.StringData[".dockerconfigjson"]
   113  }
   114  
   115  func (m *AgentManifests) finish() error {
   116  	if err := m.validateAgentManifests().ToAggregate(); err != nil {
   117  		return errors.Wrapf(err, "invalid agent configuration")
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  func (m *AgentManifests) validateAgentManifests() field.ErrorList {
   124  	allErrs := field.ErrorList{}
   125  
   126  	if err := m.validateNMStateLabelSelector(); err != nil {
   127  		allErrs = append(allErrs, err...)
   128  	}
   129  
   130  	return allErrs
   131  }
   132  
   133  func (m *AgentManifests) validateNMStateLabelSelector() field.ErrorList {
   134  
   135  	var allErrs field.ErrorList
   136  
   137  	fieldPath := field.NewPath("Spec", "NMStateConfigLabelSelector", "MatchLabels")
   138  
   139  	for _, networkConfig := range m.NMStateConfigs {
   140  		if !reflect.DeepEqual(m.InfraEnv.Spec.NMStateConfigLabelSelector.MatchLabels, networkConfig.ObjectMeta.Labels) {
   141  			allErrs = append(allErrs, field.Required(fieldPath, fmt.Sprintf("infraEnv and %s.NMStateConfig labels do not match. Expected: %s Found: %s",
   142  				networkConfig.Name,
   143  				m.InfraEnv.Spec.NMStateConfigLabelSelector.MatchLabels,
   144  				networkConfig.ObjectMeta.Labels)))
   145  		}
   146  
   147  	}
   148  
   149  	return allErrs
   150  }