...

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

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

     1  // Copyright 2022 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/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
    22  	"github.com/Azure/go-autorest/autorest"
    23  	"github.com/Azure/go-autorest/autorest/azure/auth"
    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  // GetVMClient is used to get the azure VM Client
    32  func GetVMClient(ctx context.Context, cli client.Client, azurechaos *v1alpha1.AzureChaos) (*compute.VirtualMachinesClient, error) {
    33  	authorizer, err := GetAuthorizer(ctx, cli, azurechaos)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	vmClient := compute.NewVirtualMachinesClient(azurechaos.Spec.SubscriptionID)
    39  	vmClient.Authorizer = authorizer
    40  
    41  	return &vmClient, nil
    42  }
    43  
    44  // GetDiskClient is used to get the azure disk Client
    45  func GetDiskClient(ctx context.Context, cli client.Client, azurechaos *v1alpha1.AzureChaos) (*compute.DisksClient, error) {
    46  	authorizer, err := GetAuthorizer(ctx, cli, azurechaos)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	disksClient := compute.NewDisksClient(azurechaos.Spec.SubscriptionID)
    51  	disksClient.Authorizer = authorizer
    52  
    53  	return &disksClient, nil
    54  }
    55  
    56  // GetAuthorizer is used to get the azure authorizer
    57  func GetAuthorizer(ctx context.Context, cli client.Client, azurechaos *v1alpha1.AzureChaos) (autorest.Authorizer, error) {
    58  	secret := &v1.Secret{}
    59  	err := cli.Get(ctx, types.NamespacedName{
    60  		Name:      *azurechaos.Spec.SecretName,
    61  		Namespace: azurechaos.Namespace,
    62  	}, secret)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	clientCredentialConfig := auth.NewClientCredentialsConfig(
    68  		string(secret.Data["client_id"]),
    69  		string(secret.Data["client_secret"]),
    70  		string(secret.Data["tenant_id"]))
    71  
    72  	authorizer, err := clientCredentialConfig.Authorizer()
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	return authorizer, nil
    78  }
    79