github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/matchers_kube.go (about)

     1  /*
     2  Copyright 2023 Gravitational, Inc.
     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 types
    18  
    19  import (
    20  	"slices"
    21  
    22  	"github.com/gravitational/trace"
    23  
    24  	apiutils "github.com/gravitational/teleport/api/utils"
    25  )
    26  
    27  const (
    28  	// KubernetesMatchersApp is app matcher type for Kubernetes services
    29  	KubernetesMatchersApp = "app"
    30  )
    31  
    32  // SupportedKubernetesMatchers is a list of Kubernetes matchers supported by
    33  // Teleport discovery service
    34  var SupportedKubernetesMatchers = []string{
    35  	KubernetesMatchersApp,
    36  }
    37  
    38  // CheckAndSetDefaults that the matcher is correct and adds default values.
    39  func (m *KubernetesMatcher) CheckAndSetDefaults() error {
    40  	for _, t := range m.Types {
    41  		if !slices.Contains(SupportedKubernetesMatchers, t) {
    42  			return trace.BadParameter("Kubernetes discovery does not support %q resource type; supported resource types are: %v",
    43  				t, SupportedKubernetesMatchers)
    44  		}
    45  	}
    46  
    47  	if len(m.Types) == 0 {
    48  		m.Types = []string{KubernetesMatchersApp}
    49  	}
    50  
    51  	if len(m.Namespaces) == 0 {
    52  		m.Namespaces = []string{Wildcard}
    53  	}
    54  
    55  	if len(m.Labels) == 0 {
    56  		m.Labels = map[string]apiutils.Strings{Wildcard: {Wildcard}}
    57  	}
    58  
    59  	return nil
    60  }