github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/render/validate/validate.go (about)

     1  /*
     2  Copyright 2021 The Skaffold 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 validate
    18  
    19  import (
    20  	"fmt"
    21  
    22  	sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/render/kptfile"
    24  	latestV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v2"
    25  	"github.com/GoogleContainerTools/skaffold/proto/v1"
    26  )
    27  
    28  var (
    29  	allowListedValidators = []string{"kubeval"}
    30  	validatorAllowlist    = map[string]kptfile.Function{
    31  		"kubeval": {Image: "gcr.io/kpt-fn/kubeval:v0.1"},
    32  		// TODO: Add conftest validator in kpt catalog.
    33  	}
    34  )
    35  
    36  // NewValidator instantiates a Validator object.
    37  func NewValidator(config []latestV2.Validator) (*Validator, error) {
    38  	var fns []kptfile.Function
    39  	for _, c := range config {
    40  		fn, ok := validatorAllowlist[c.Name]
    41  		if !ok {
    42  			// TODO: Add links to explain "skaffold-managed mode" and "kpt-managed mode".
    43  			return nil, sErrors.NewErrorWithStatusCode(
    44  				&proto.ActionableErr{
    45  					Message: fmt.Sprintf("unsupported validator %q", c.Name),
    46  					ErrCode: proto.StatusCode_CONFIG_UNKNOWN_VALIDATOR,
    47  					Suggestions: []*proto.Suggestion{
    48  						{
    49  							SuggestionCode: proto.SuggestionCode_CONFIG_ALLOWLIST_VALIDATORS,
    50  							Action: fmt.Sprintf(
    51  								"please only use the following validators in skaffold-managed mode: %v. "+
    52  									"to use custom validators, please use kpt-managed mode.", allowListedValidators),
    53  						},
    54  					},
    55  				})
    56  		}
    57  		fns = append(fns, fn)
    58  	}
    59  	return &Validator{kptFn: fns}, nil
    60  }
    61  
    62  type Validator struct {
    63  	kptFn []kptfile.Function
    64  }
    65  
    66  // GetDeclarativeValidators transforms and returns the skaffold validators defined in skaffold.yaml
    67  func (v *Validator) GetDeclarativeValidators() []kptfile.Function {
    68  	// TODO: guarantee the v.kptFn is updated once users changed skaffold.yaml file.
    69  	return v.kptFn
    70  }