istio.io/istio@v0.0.0-20240520182934-d79c90f27776/cni/pkg/repair/repair.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 repair
    16  
    17  import (
    18  	"context"
    19  
    20  	"istio.io/istio/cni/pkg/config"
    21  	"istio.io/istio/pkg/kube"
    22  	"istio.io/istio/pkg/log"
    23  )
    24  
    25  var repairLog = log.RegisterScope("repair", "CNI race condition repair")
    26  
    27  func StartRepair(ctx context.Context, cfg config.RepairConfig) {
    28  	if !cfg.Enabled {
    29  		repairLog.Info("CNI repair is disable.")
    30  		return
    31  	}
    32  	repairLog.Info("Start CNI race condition repair.")
    33  
    34  	client, err := clientSetup()
    35  	if err != nil {
    36  		repairLog.Fatalf("CNI repair could not construct clientSet: %s", err)
    37  	}
    38  
    39  	rc, err := NewRepairController(client, cfg)
    40  	if err != nil {
    41  		repairLog.Fatalf("Fatal error constructing repair controller: %+v", err)
    42  	}
    43  	go rc.Run(ctx.Done())
    44  	client.RunAndWait(ctx.Done())
    45  }
    46  
    47  // Set up Kubernetes client using kubeconfig (or in-cluster config if no file provided)
    48  func clientSetup() (kube.Client, error) {
    49  	config, err := kube.DefaultRestConfig("", "")
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return kube.NewClient(kube.NewClientConfigForRestConfig(config), "")
    54  }