sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/common.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes 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 client
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	"github.com/pkg/errors"
    24  	"k8s.io/apimachinery/pkg/util/validation"
    25  
    26  	clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    27  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
    28  )
    29  
    30  // getComponentsByName is a utility method that returns components
    31  // for a given provider with options including targetNamespace.
    32  func (c *clusterctlClient) getComponentsByName(ctx context.Context, provider string, providerType clusterctlv1.ProviderType, options repository.ComponentsOptions) (repository.Components, error) {
    33  	// Parse the abbreviated syntax for name[:version]
    34  	name, version, err := parseProviderName(provider)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	options.Version = version
    39  
    40  	// Gets the provider configuration (that includes the location of the provider repository)
    41  	providerConfig, err := c.configClient.Providers().Get(name, providerType)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	// Get a client for the provider repository and read the provider components;
    47  	// during the process, provider components will be processed performing variable substitution, customization of target
    48  	// namespace etc.
    49  	// Currently we are not supporting custom yaml processors for the provider
    50  	// components. So we revert to using the default SimpleYamlProcessor.
    51  	repositoryClientFactory, err := c.repositoryClientFactory(ctx, RepositoryClientFactoryInput{Provider: providerConfig})
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	components, err := repositoryClientFactory.Components().Get(ctx, options)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	return components, nil
    61  }
    62  
    63  // parseProviderName defines a utility function that parses the abbreviated syntax for name[:version].
    64  func parseProviderName(provider string) (name string, version string, err error) {
    65  	t := strings.Split(strings.ToLower(provider), ":")
    66  	if len(t) > 2 {
    67  		return "", "", errors.Errorf("invalid provider name %q. Provider name should be in the form name[:version]", provider)
    68  	}
    69  
    70  	if t[0] == "" {
    71  		return "", "", errors.Errorf("invalid provider name %q. Provider name should be in the form name[:version] and name cannot be empty", provider)
    72  	}
    73  
    74  	name = t[0]
    75  	if err := validateDNS1123Label(name); err != nil {
    76  		return "", "", errors.Wrapf(err, "invalid provider name %q. Provider name should be in the form name[:version] and the name should be valid", provider)
    77  	}
    78  
    79  	version = ""
    80  	if len(t) > 1 {
    81  		if t[1] == "" {
    82  			return "", "", errors.Errorf("invalid provider name %q. Provider name should be in the form name[:version] and version cannot be empty", provider)
    83  		}
    84  		version = t[1]
    85  	}
    86  
    87  	return name, version, nil
    88  }
    89  
    90  func validateDNS1123Label(label string) error {
    91  	if errs := validation.IsDNS1123Label(label); len(errs) != 0 {
    92  		return errors.New(strings.Join(errs, "; "))
    93  	}
    94  	return nil
    95  }
    96  
    97  func validateDNS1123Domanin(subdomain string) error {
    98  	if errs := validation.IsDNS1123Subdomain(subdomain); len(errs) != 0 {
    99  		return errors.New(strings.Join(errs, "; "))
   100  	}
   101  	return nil
   102  }