...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/podnetworkchaos/tc/tc.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/podnetworkchaos/tc

     1  // Copyright 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package tc
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	v1 "k8s.io/api/core/v1"
    21  	"sigs.k8s.io/controller-runtime/pkg/client"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/controllers/utils"
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    25  )
    26  
    27  // SetTcs makes grpc call to chaosdaemon to flush traffic control rules
    28  func SetTcs(ctx context.Context, c client.Client, pod *v1.Pod, tcs []*pb.Tc) error {
    29  	pbClient, err := utils.NewChaosDaemonClient(ctx, c, pod)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	defer pbClient.Close()
    34  
    35  	if len(pod.Status.ContainerStatuses) == 0 {
    36  		return fmt.Errorf("%s %s can't get the state of container", pod.Namespace, pod.Name)
    37  	}
    38  
    39  	containerID := pod.Status.ContainerStatuses[0].ContainerID
    40  
    41  	_, err = pbClient.SetTcs(ctx, &pb.TcsRequest{
    42  		Tcs:         tcs,
    43  		ContainerId: containerID,
    44  		// Prevent tcs is empty, used to clean up tc rules
    45  		Device:  "eth0",
    46  		EnterNS: true,
    47  	})
    48  	return err
    49  }
    50