istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/cmd/pilot-agent/status/grpcready/probe.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 grpcready
    16  
    17  import (
    18  	"fmt"
    19  	"sync"
    20  
    21  	"istio.io/istio/pilot/cmd/pilot-agent/status/ready"
    22  	"istio.io/istio/pkg/file"
    23  	"istio.io/istio/pkg/istio-agent/grpcxds"
    24  )
    25  
    26  var _ ready.Prober = &probe{}
    27  
    28  type probe struct {
    29  	sync.RWMutex
    30  	bootstrapPath string
    31  	bootstrap     *grpcxds.Bootstrap
    32  }
    33  
    34  // NewProbe returns a probe that checks if a valid bootstrap file can be loaded once.
    35  // If that bootstrap file has a file_watcher cert provider, we also ensure those certs exist.
    36  func NewProbe(bootstrapPath string) ready.Prober {
    37  	return &probe{bootstrapPath: bootstrapPath}
    38  }
    39  
    40  func (p *probe) Check() error {
    41  	// TODO file watch?
    42  	if p.getBootstrap() == nil {
    43  		bootstrap, err := grpcxds.LoadBootstrap(p.bootstrapPath)
    44  		if err != nil {
    45  			return fmt.Errorf("failed loading %s: %v", p.bootstrapPath, err)
    46  		}
    47  		p.setBootstrap(bootstrap)
    48  	}
    49  	if bootstrap := p.getBootstrap(); bootstrap != nil {
    50  		if fwp := bootstrap.FileWatcherProvider(); fwp != nil {
    51  			for _, path := range fwp.FilePaths() {
    52  				if !file.Exists(path) {
    53  					return fmt.Errorf("%s does not exist", path)
    54  				}
    55  			}
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func (p *probe) getBootstrap() *grpcxds.Bootstrap {
    62  	p.RLock()
    63  	defer p.RUnlock()
    64  	return p.bootstrap
    65  }
    66  
    67  func (p *probe) setBootstrap(bootstrap *grpcxds.Bootstrap) {
    68  	p.Lock()
    69  	defer p.Unlock()
    70  	p.bootstrap = bootstrap
    71  }