istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/analyzers/serviceentry/protocoladdresses.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  
    15  package serviceentry
    16  
    17  import (
    18  	"fmt"
    19  
    20  	meshconfig "istio.io/api/mesh/v1alpha1"
    21  	"istio.io/api/networking/v1alpha3"
    22  	"istio.io/istio/pkg/config"
    23  	"istio.io/istio/pkg/config/analysis"
    24  	"istio.io/istio/pkg/config/analysis/analyzers/util"
    25  	"istio.io/istio/pkg/config/analysis/msg"
    26  	"istio.io/istio/pkg/config/resource"
    27  	"istio.io/istio/pkg/config/schema/gvk"
    28  )
    29  
    30  type ProtocolAddressesAnalyzer struct{}
    31  
    32  var _ analysis.Analyzer = &ProtocolAddressesAnalyzer{}
    33  
    34  func (serviceEntry *ProtocolAddressesAnalyzer) Metadata() analysis.Metadata {
    35  	return analysis.Metadata{
    36  		Name:        "serviceentry.Analyzer",
    37  		Description: "Checks the validity of ServiceEntry",
    38  		Inputs: []config.GroupVersionKind{
    39  			gvk.ServiceEntry,
    40  			gvk.MeshConfig,
    41  		},
    42  	}
    43  }
    44  
    45  func (serviceEntry *ProtocolAddressesAnalyzer) Analyze(context analysis.Context) {
    46  	autoAllocated := false
    47  	context.ForEach(gvk.MeshConfig, func(r *resource.Instance) bool {
    48  		mc := r.Message.(*meshconfig.MeshConfig)
    49  		if v, ok := mc.DefaultConfig.ProxyMetadata["ISTIO_META_DNS_CAPTURE"]; !ok || v != "true" {
    50  			return true
    51  		}
    52  		if v, ok := mc.DefaultConfig.ProxyMetadata["ISTIO_META_DNS_AUTO_ALLOCATE"]; ok && v == "true" {
    53  			autoAllocated = true
    54  		}
    55  		return true
    56  	})
    57  
    58  	context.ForEach(gvk.ServiceEntry, func(resource *resource.Instance) bool {
    59  		serviceEntry.analyzeProtocolAddresses(resource, context, autoAllocated)
    60  		return true
    61  	})
    62  }
    63  
    64  func (serviceEntry *ProtocolAddressesAnalyzer) analyzeProtocolAddresses(r *resource.Instance, ctx analysis.Context, metaDNSAutoAllocated bool) {
    65  	se := r.Message.(*v1alpha3.ServiceEntry)
    66  	if se.Addresses == nil && !metaDNSAutoAllocated {
    67  		for index, port := range se.Ports {
    68  			if port.Protocol == "" || port.Protocol == "TCP" {
    69  				message := msg.NewServiceEntryAddressesRequired(r)
    70  
    71  				if line, ok := util.ErrorLine(r, fmt.Sprintf(util.ServiceEntryPort, index)); ok {
    72  					message.Line = line
    73  				}
    74  
    75  				ctx.Report(gvk.ServiceEntry, message)
    76  			}
    77  		}
    78  	}
    79  }