istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/analyzers/telemetry/default_selector.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package telemetry
    15  
    16  import (
    17  	"istio.io/api/telemetry/v1alpha1"
    18  	"istio.io/istio/pkg/config"
    19  	"istio.io/istio/pkg/config/analysis"
    20  	"istio.io/istio/pkg/config/analysis/msg"
    21  	"istio.io/istio/pkg/config/resource"
    22  	"istio.io/istio/pkg/config/schema/gvk"
    23  )
    24  
    25  // DefaultSelectorAnalyzer validates, per namespace, that there aren't multiple
    26  // telemetry resources that have no selector. This is distinct from
    27  // SelectorAnalyzer because it does not require pods, so it can run even if that
    28  // collection is unavailable.
    29  type DefaultSelectorAnalyzer struct{}
    30  
    31  var _ analysis.Analyzer = &DefaultSelectorAnalyzer{}
    32  
    33  // Metadata implements Analyzer
    34  func (a *DefaultSelectorAnalyzer) Metadata() analysis.Metadata {
    35  	return analysis.Metadata{
    36  		Name:        "telemetry.DefaultSelectorAnalyzer",
    37  		Description: "Validates that there aren't multiple telemetry resources that have no selector",
    38  		Inputs: []config.GroupVersionKind{
    39  			gvk.Telemetry,
    40  		},
    41  	}
    42  }
    43  
    44  // Analyze implements Analyzer
    45  func (a *DefaultSelectorAnalyzer) Analyze(c analysis.Context) {
    46  	nsToTelemetries := make(map[resource.Namespace][]*resource.Instance)
    47  
    48  	c.ForEach(gvk.Telemetry, func(r *resource.Instance) bool {
    49  		s := r.Message.(*v1alpha1.Telemetry)
    50  
    51  		ns := r.Metadata.FullName.Namespace
    52  
    53  		if s.Selector == nil {
    54  			nsToTelemetries[ns] = append(nsToTelemetries[ns], r)
    55  		}
    56  		return true
    57  	})
    58  
    59  	// Check for more than one selector-less telemetry instance, per namespace
    60  	for ns, sList := range nsToTelemetries {
    61  		if len(sList) > 1 {
    62  			sNames := getNames(sList)
    63  			for _, r := range sList {
    64  				c.Report(gvk.Telemetry, msg.NewMultipleTelemetriesWithoutWorkloadSelectors(r, sNames, string(ns)))
    65  			}
    66  		}
    67  	}
    68  }