sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1beta1/s3bucket.go (about)

     1  /*
     2  Copyright 2021 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 v1beta1
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  
    25  	"sigs.k8s.io/cluster-api-provider-aws/feature"
    26  )
    27  
    28  // Validate validates S3Bucket fields.
    29  func (b *S3Bucket) Validate() []*field.Error {
    30  	var errs field.ErrorList
    31  
    32  	if b == nil {
    33  		return errs
    34  	}
    35  
    36  	if b.Name == "" {
    37  		errs = append(errs, field.Required(field.NewPath("spec", "s3Bucket", "name"), "can't be empty"))
    38  	}
    39  
    40  	// Feature gate is not enabled but ignition is enabled then send a forbidden error.
    41  	if !feature.Gates.Enabled(feature.BootstrapFormatIgnition) {
    42  		errs = append(errs, field.Forbidden(field.NewPath("spec", "s3Bucket"),
    43  			"can be set only if the BootstrapFormatIgnition feature gate is enabled"))
    44  	}
    45  
    46  	if b.ControlPlaneIAMInstanceProfile == "" {
    47  		errs = append(errs,
    48  			field.Required(field.NewPath("spec", "s3Bucket", "controlPlaneIAMInstanceProfiles"), "can't be empty"))
    49  	}
    50  
    51  	if len(b.NodesIAMInstanceProfiles) == 0 {
    52  		errs = append(errs,
    53  			field.Required(field.NewPath("spec", "s3Bucket", "nodesIAMInstanceProfiles"), "can't be empty"))
    54  	}
    55  
    56  	for i, iamInstanceProfile := range b.NodesIAMInstanceProfiles {
    57  		if iamInstanceProfile == "" {
    58  			errs = append(errs,
    59  				field.Required(field.NewPath("spec", "s3Bucket", fmt.Sprintf("nodesIAMInstanceProfiles[%d]", i)), "can't be empty"))
    60  		}
    61  	}
    62  
    63  	if b.Name != "" {
    64  		errs = append(errs, validateS3BucketName(b.Name)...)
    65  	}
    66  
    67  	return errs
    68  }
    69  
    70  // Validation rules taken from https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html.
    71  func validateS3BucketName(name string) []*field.Error {
    72  	var errs field.ErrorList
    73  
    74  	path := field.NewPath("spec", "s3Bucket", "name")
    75  
    76  	if net.ParseIP(name) != nil {
    77  		errs = append(errs, field.Invalid(path, name, "must not be formatted as an IP address (for example, 192.168.5.4)"))
    78  	}
    79  
    80  	return errs
    81  }