...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/ctrl/client/forward.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/ctrl/client

     1  // Copyright 2021 Chaos Mesh 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  
    16  package client
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/pkg/errors"
    22  	"k8s.io/cli-runtime/pkg/genericclioptions"
    23  	"k8s.io/client-go/rest"
    24  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/portforward"
    28  )
    29  
    30  const (
    31  	CtrlServerPort = 10082
    32  )
    33  
    34  // CommonRestClientGetter is used for non-e2e test environment.
    35  // It's basically do the same thing as genericclioptions.ConfigFlags, but it load rest config from incluster or .kubeconfig file
    36  type CommonRestClientGetter struct {
    37  	*genericclioptions.ConfigFlags
    38  }
    39  
    40  func NewCommonRestClientGetter() *CommonRestClientGetter {
    41  	innerConfigFlags := genericclioptions.NewConfigFlags(false)
    42  	return &CommonRestClientGetter{innerConfigFlags}
    43  }
    44  
    45  func (it *CommonRestClientGetter) ToRESTConfig() (*rest.Config, error) {
    46  	return config.GetConfig()
    47  }
    48  
    49  func ForwardSvcPorts(ctx context.Context, ns, svc string, port uint16) (context.CancelFunc, uint16, error) {
    50  	commonRestClientGetter := NewCommonRestClientGetter()
    51  	logger, err := log.NewDefaultZapLogger()
    52  	if err != nil {
    53  		return nil, 0, errors.Wrap(err, "failed to create logger")
    54  	}
    55  	fw, err := portforward.NewPortForwarder(ctx, commonRestClientGetter, false, logger)
    56  	if err != nil {
    57  		return nil, 0, errors.Wrap(err, "failed to create port forwarder")
    58  	}
    59  	_, localPort, pfCancel, err := portforward.ForwardOnePort(fw, ns, svc, port)
    60  
    61  	// disable error handler in k8s runtime to prevent complaining from port forwarder
    62  	DisableRuntimeErrorHandler()
    63  	return pfCancel, localPort, err
    64  }
    65  
    66  func ForwardCtrlServer(ctx context.Context, ns, managerSvc string) (context.CancelFunc, uint16, error) {
    67  	return ForwardSvcPorts(ctx, ns, "svc/"+managerSvc, CtrlServerPort)
    68  }
    69