...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/utils/record.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/utils

     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 utils
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/pkg/errors"
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/types"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    27  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/chaosdaemon"
    28  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/controller"
    29  	chaosdaemonclient "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/client"
    30  )
    31  
    32  type ContainerRecordDecoder struct {
    33  	client.Client
    34  	*chaosdaemon.ChaosDaemonClientBuilder
    35  }
    36  
    37  func NewContainerRecordDecoder(c client.Client, builder *chaosdaemon.ChaosDaemonClientBuilder) *ContainerRecordDecoder {
    38  	return &ContainerRecordDecoder{
    39  		Client:                   c,
    40  		ChaosDaemonClientBuilder: builder,
    41  	}
    42  }
    43  
    44  type DecodedContainerRecord struct {
    45  	PbClient      chaosdaemonclient.ChaosDaemonClientInterface
    46  	ContainerId   string
    47  	ContainerName string
    48  	Pod           *v1.Pod
    49  }
    50  
    51  func (d *ContainerRecordDecoder) DecodeContainerRecord(ctx context.Context, record *v1alpha1.Record, obj v1alpha1.InnerObject) (decoded DecodedContainerRecord, err error) {
    52  	var pod v1.Pod
    53  	podId, containerName, err := controller.ParseNamespacedNameContainer(record.Id)
    54  	if err != nil {
    55  		err = errors.Wrapf(ErrContainerNotFound, "container with id %s not found", record.Id)
    56  		return
    57  	}
    58  	err = d.Client.Get(ctx, podId, &pod)
    59  	if err != nil {
    60  		err = errors.Wrapf(ErrContainerNotFound, "container with id %s not found", record.Id)
    61  		return
    62  	}
    63  	decoded.Pod = &pod
    64  	if len(pod.Status.ContainerStatuses) == 0 {
    65  		err = errors.Wrapf(ErrContainerNotFound, "container with id %s not found", record.Id)
    66  		return
    67  	}
    68  
    69  	for _, container := range pod.Status.ContainerStatuses {
    70  		if container.Name == containerName {
    71  			decoded.ContainerId = container.ContainerID
    72  			decoded.ContainerName = containerName
    73  			break
    74  		}
    75  	}
    76  	if len(decoded.ContainerId) == 0 {
    77  		err = errors.Wrapf(ErrContainerNotFound, "container with id %s not found", record.Id)
    78  		return
    79  	}
    80  
    81  	decoded.PbClient, err = d.ChaosDaemonClientBuilder.Build(ctx, &pod, &types.NamespacedName{
    82  		Namespace: obj.GetNamespace(),
    83  		Name:      obj.GetName(),
    84  	})
    85  	if err != nil {
    86  		return
    87  	}
    88  
    89  	return
    90  }
    91