sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1alpha4/validate.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 v1alpha4
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  )
    25  
    26  // Validate will validate the bastion fields.
    27  func (b *Bastion) Validate() []*field.Error {
    28  	var errs field.ErrorList
    29  
    30  	if b.DisableIngressRules && len(b.AllowedCIDRBlocks) > 0 {
    31  		errs = append(errs,
    32  			field.Forbidden(field.NewPath("spec", "bastion", "allowedCIDRBlocks"), "cannot be set if spec.bastion.disableIngressRules is true"),
    33  		)
    34  		return errs
    35  	}
    36  
    37  	for i, cidr := range b.AllowedCIDRBlocks {
    38  		if _, _, err := net.ParseCIDR(cidr); err != nil {
    39  			errs = append(errs,
    40  				field.Invalid(field.NewPath("spec", "bastion", fmt.Sprintf("allowedCIDRBlocks[%d]", i)), cidr, "must be a valid CIDR block"),
    41  			)
    42  		}
    43  	}
    44  	return errs
    45  }