sigs.k8s.io/cluster-api-provider-azure@v1.14.3/api/v1beta1/azureimage_validation.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  	"k8s.io/apimachinery/pkg/util/validation/field"
    21  )
    22  
    23  // ValidateImage validates an image.
    24  func ValidateImage(image *Image, fldPath *field.Path) field.ErrorList {
    25  	allErrs := field.ErrorList{}
    26  
    27  	if image == nil {
    28  		// allow empty image as it is defaulted in the AzureMachine controller
    29  		return allErrs
    30  	}
    31  
    32  	allErrs = append(allErrs, validateSingleDetailsOnly(image, fldPath)...)
    33  
    34  	if image.Marketplace != nil {
    35  		allErrs = append(allErrs, validateMarketplaceImage(image, fldPath)...)
    36  	}
    37  	if image.SharedGallery != nil {
    38  		allErrs = append(allErrs, validateSharedGalleryImage(image, fldPath)...)
    39  	}
    40  	if image.ID != nil {
    41  		allErrs = append(allErrs, validateSpecificImage(image, fldPath)...)
    42  	}
    43  	if image.ComputeGallery != nil {
    44  		allErrs = append(allErrs, validateComputeGalleryImage(image, fldPath)...)
    45  	}
    46  
    47  	return allErrs
    48  }
    49  
    50  func validateSingleDetailsOnly(image *Image, fldPath *field.Path) field.ErrorList {
    51  	allErrs := field.ErrorList{}
    52  	imageDetailsFound := false
    53  
    54  	if image.ID != nil {
    55  		imageDetailsFound = true
    56  	}
    57  
    58  	if image.Marketplace != nil {
    59  		if imageDetailsFound {
    60  			allErrs = append(allErrs, field.Forbidden(fldPath.Child("Marketplace"), "Marketplace cannot be used as an image ID has been specified"))
    61  		} else {
    62  			imageDetailsFound = true
    63  		}
    64  	}
    65  
    66  	if image.SharedGallery != nil {
    67  		if imageDetailsFound {
    68  			allErrs = append(allErrs, field.Forbidden(fldPath.Child("SharedGallery"), "SharedGallery cannot be used as an image ID. Marketplace or ComputeGallery images has been specified"))
    69  		} else {
    70  			imageDetailsFound = true
    71  		}
    72  	}
    73  
    74  	if image.ComputeGallery != nil {
    75  		if imageDetailsFound {
    76  			allErrs = append(allErrs, field.Forbidden(fldPath.Child("ComputeGallery"), "ComputeGallery cannot be used as an image ID. Marketplace or SharedGallery images has been specified"))
    77  		} else {
    78  			imageDetailsFound = true
    79  		}
    80  	}
    81  
    82  	if !imageDetailsFound {
    83  		allErrs = append(allErrs, field.Required(fldPath, "You must supply an ID, Marketplace or ComputeGallery image details"))
    84  	}
    85  
    86  	return allErrs
    87  }
    88  
    89  func validateComputeGalleryImage(image *Image, fldPath *field.Path) field.ErrorList {
    90  	allErrs := field.ErrorList{}
    91  
    92  	if image.ComputeGallery.SubscriptionID != nil && image.ComputeGallery.ResourceGroup == nil {
    93  		allErrs = append(allErrs, field.Invalid(fldPath.Child("ResourceGroup"), "", "ResourceGroup cannot be empty when SubscriptionID is specified"))
    94  	}
    95  	if image.ComputeGallery.ResourceGroup != nil && image.ComputeGallery.SubscriptionID == nil {
    96  		allErrs = append(allErrs, field.Invalid(fldPath.Child("SubscriptionID"), "", "SubscriptionID cannot be empty when ResourceGroup is specified"))
    97  	}
    98  
    99  	return allErrs
   100  }
   101  
   102  func validateSharedGalleryImage(image *Image, fldPath *field.Path) field.ErrorList {
   103  	allErrs := field.ErrorList{}
   104  
   105  	if image.SharedGallery.SubscriptionID == "" {
   106  		allErrs = append(allErrs, field.Invalid(fldPath.Child("SubscriptionID"), "", "SubscriptionID cannot be empty when specifying an AzureSharedGalleryImage"))
   107  	}
   108  	if image.SharedGallery.ResourceGroup == "" {
   109  		allErrs = append(allErrs, field.Invalid(fldPath.Child("ResourceGroup"), "", "ResourceGroup cannot be empty when specifying an AzureSharedGalleryImage"))
   110  	}
   111  	if image.SharedGallery.Gallery == "" {
   112  		allErrs = append(allErrs, field.Invalid(fldPath.Child("Gallery"), "", "Gallery cannot be empty when specifying an AzureSharedGalleryImage"))
   113  	}
   114  	if image.SharedGallery.Name == "" {
   115  		allErrs = append(allErrs, field.Invalid(fldPath.Child("Name"), "", "Name cannot be empty when specifying an AzureSharedGalleryImage"))
   116  	}
   117  	if image.SharedGallery.Version == "" {
   118  		allErrs = append(allErrs, field.Invalid(fldPath.Child("Version"), "", "Version cannot be empty when specifying an AzureSharedGalleryImage"))
   119  	}
   120  
   121  	return allErrs
   122  }
   123  
   124  func validateMarketplaceImage(image *Image, fldPath *field.Path) field.ErrorList {
   125  	allErrs := field.ErrorList{}
   126  
   127  	if image.Marketplace.Publisher == "" {
   128  		allErrs = append(allErrs, field.Invalid(fldPath.Child("Publisher"), "", "Publisher cannot be empty when specifying an AzureMarketplaceImage"))
   129  	}
   130  	if image.Marketplace.Offer == "" {
   131  		allErrs = append(allErrs, field.Invalid(fldPath.Child("Offer"), "", "Offer cannot be empty when specifying an AzureMarketplaceImage"))
   132  	}
   133  	if image.Marketplace.SKU == "" {
   134  		allErrs = append(allErrs, field.Invalid(fldPath.Child("SKU"), "", "SKU cannot be empty when specifying an AzureMarketplaceImage"))
   135  	}
   136  	if image.Marketplace.Version == "" {
   137  		allErrs = append(allErrs, field.Invalid(fldPath.Child("Version"), "", "Version cannot be empty when specifying an AzureMarketplaceImage"))
   138  	}
   139  	return allErrs
   140  }
   141  
   142  func validateSpecificImage(image *Image, fldPath *field.Path) field.ErrorList {
   143  	allErrs := field.ErrorList{}
   144  
   145  	if *image.ID == "" {
   146  		allErrs = append(allErrs, field.Invalid(fldPath.Child("ID"), "", "ID cannot be empty when specifying an AzureImageByID"))
   147  	}
   148  
   149  	return allErrs
   150  }