...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/awschaos/ec2restart/impl.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/awschaos/ec2restart

     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 ec2restart
    15  
    16  import (
    17  	"context"
    18  	"encoding/json"
    19  
    20  	awscfg "github.com/aws/aws-sdk-go-v2/config"
    21  	"github.com/aws/aws-sdk-go-v2/credentials"
    22  	"github.com/aws/aws-sdk-go-v2/service/ec2"
    23  	"github.com/go-logr/logr"
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    29  )
    30  
    31  type Impl struct {
    32  	client.Client
    33  
    34  	Log logr.Logger
    35  }
    36  
    37  func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    38  	awschaos := obj.(*v1alpha1.AWSChaos)
    39  
    40  	var selected v1alpha1.AWSSelector
    41  	json.Unmarshal([]byte(records[index].Id), &selected)
    42  	opts := []func(*awscfg.LoadOptions) error{
    43  		awscfg.WithRegion(selected.AWSRegion),
    44  	}
    45  
    46  	if awschaos.Spec.SecretName != nil {
    47  		secret := &v1.Secret{}
    48  		err := impl.Client.Get(ctx, types.NamespacedName{
    49  			Name:      *awschaos.Spec.SecretName,
    50  			Namespace: awschaos.Namespace,
    51  		}, secret)
    52  		if err != nil {
    53  			impl.Log.Error(err, "fail to get cloud secret")
    54  			return v1alpha1.NotInjected, err
    55  		}
    56  		opts = append(opts, awscfg.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
    57  			string(secret.Data["aws_access_key_id"]),
    58  			string(secret.Data["aws_secret_access_key"]),
    59  			"",
    60  		)))
    61  	}
    62  	cfg, err := awscfg.LoadDefaultConfig(ctx, opts...)
    63  	if err != nil {
    64  		impl.Log.Error(err, "unable to load aws SDK config")
    65  		return v1alpha1.NotInjected, err
    66  	}
    67  	ec2client := ec2.NewFromConfig(cfg)
    68  
    69  	_, err = ec2client.RebootInstances(context.TODO(), &ec2.RebootInstancesInput{
    70  		InstanceIds: []string{selected.Ec2Instance},
    71  	})
    72  
    73  	if err != nil {
    74  		impl.Log.Error(err, "fail to restart the instance")
    75  		return v1alpha1.NotInjected, err
    76  	}
    77  
    78  	return v1alpha1.Injected, nil
    79  }
    80  
    81  func (impl *Impl) Recover(_ context.Context, _ int, _ []*v1alpha1.Record, _ v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    82  	return v1alpha1.NotInjected, nil
    83  }
    84  
    85  func NewImpl(c client.Client, log logr.Logger) *Impl {
    86  	return &Impl{
    87  		Client: c,
    88  		Log:    log.WithName("ec2restart"),
    89  	}
    90  }
    91