github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/provisioner/fake_client.go (about)

     1  package provisioner
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"sync"
     8  
     9  	"github.com/google/uuid"
    10  	schema "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
    11  	"github.com/kyma-project/kyma-environment-broker/common/gardener"
    12  	"github.com/kyma-project/kyma-environment-broker/internal"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/ptr"
    14  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    16  	"k8s.io/client-go/dynamic"
    17  )
    18  
    19  type runtime struct {
    20  	runtimeInput schema.ProvisionRuntimeInput
    21  }
    22  
    23  type FakeClient struct {
    24  	mu          sync.Mutex
    25  	graphqlizer Graphqlizer
    26  
    27  	runtimes      []runtime
    28  	upgrades      map[string]schema.UpgradeRuntimeInput
    29  	shootUpgrades map[string]schema.UpgradeShootInput
    30  	operations    map[string]schema.OperationStatus
    31  	dumpRequest   bool
    32  
    33  	gardenerClient    dynamic.Interface
    34  	gardenerNamespace string
    35  }
    36  
    37  func NewFakeClient() *FakeClient {
    38  	return NewFakeClientWithGardener(nil, "")
    39  }
    40  
    41  func NewFakeClientWithGardener(gc dynamic.Interface, ns string) *FakeClient {
    42  	return &FakeClient{
    43  		graphqlizer:    Graphqlizer{},
    44  		runtimes:       []runtime{},
    45  		operations:     make(map[string]schema.OperationStatus),
    46  		upgrades:       make(map[string]schema.UpgradeRuntimeInput),
    47  		shootUpgrades:  make(map[string]schema.UpgradeShootInput),
    48  		gardenerClient: gc,
    49  	}
    50  }
    51  
    52  func (c *FakeClient) EnableRequestDumping() {
    53  	c.dumpRequest = true
    54  }
    55  
    56  func (c *FakeClient) GetLatestProvisionRuntimeInput() schema.ProvisionRuntimeInput {
    57  	c.mu.Lock()
    58  	defer c.mu.Unlock()
    59  
    60  	r := c.runtimes[len(c.runtimes)-1]
    61  	return r.runtimeInput
    62  }
    63  
    64  func (c *FakeClient) FinishProvisionerOperation(id string, state schema.OperationState) {
    65  	c.mu.Lock()
    66  	defer c.mu.Unlock()
    67  
    68  	op := c.operations[id]
    69  	op.State = state
    70  	c.operations[id] = op
    71  }
    72  
    73  func (c *FakeClient) FindOperationByRuntimeIDAndType(runtimeID string, operationType schema.OperationType) schema.OperationStatus {
    74  	c.mu.Lock()
    75  	defer c.mu.Unlock()
    76  
    77  	for _, status := range c.operations {
    78  		if *status.RuntimeID == runtimeID && status.Operation == operationType {
    79  			return status
    80  		}
    81  	}
    82  	return schema.OperationStatus{}
    83  }
    84  
    85  func (c *FakeClient) FindOperationByProvisionerOperationID(provisionerOperationID string) schema.OperationStatus {
    86  	c.mu.Lock()
    87  	defer c.mu.Unlock()
    88  
    89  	for key, status := range c.operations {
    90  		if key == provisionerOperationID {
    91  			return status
    92  		}
    93  	}
    94  	return schema.OperationStatus{}
    95  }
    96  
    97  func (c *FakeClient) SetOperation(id string, operation schema.OperationStatus) {
    98  	c.mu.Lock()
    99  	defer c.mu.Unlock()
   100  
   101  	c.operations[id] = operation
   102  }
   103  
   104  // Provisioner Client methods
   105  
   106  func (c *FakeClient) ProvisionRuntime(accountID, subAccountID string, config schema.ProvisionRuntimeInput) (schema.OperationStatus, error) {
   107  	rid := uuid.New().String()
   108  	opId := uuid.New().String()
   109  
   110  	return c.ProvisionRuntimeWithIDs(accountID, subAccountID, rid, opId, config)
   111  }
   112  
   113  func (c *FakeClient) Provision(operation internal.ProvisioningOperation) (schema.OperationStatus, error) {
   114  	input, err := operation.InputCreator.CreateProvisionClusterInput()
   115  
   116  	if err != nil {
   117  		return schema.OperationStatus{}, err
   118  	}
   119  
   120  	return c.ProvisionRuntimeWithIDs(operation.GlobalAccountID, operation.SubAccountID, operation.RuntimeID, operation.ID, input)
   121  }
   122  
   123  func (c *FakeClient) ProvisionRuntimeWithIDs(accountID, subAccountID, runtimeID, operationID string, config schema.ProvisionRuntimeInput) (schema.OperationStatus, error) {
   124  	c.mu.Lock()
   125  	defer c.mu.Unlock()
   126  
   127  	if c.dumpRequest {
   128  		gql, _ := c.graphqlizer.ProvisionRuntimeInputToGraphQL(config)
   129  		fmt.Println(gql)
   130  	}
   131  
   132  	c.runtimes = append(c.runtimes, runtime{
   133  		runtimeInput: config,
   134  	})
   135  	c.operations[operationID] = schema.OperationStatus{
   136  		ID:        &operationID,
   137  		RuntimeID: &runtimeID,
   138  		Operation: schema.OperationTypeProvision,
   139  		State:     schema.OperationStateInProgress,
   140  	}
   141  
   142  	if c.gardenerClient != nil {
   143  		shoot := unstructured.Unstructured{
   144  			Object: map[string]interface{}{
   145  				"apiVersion": "core.gardener.cloud/v1beta1",
   146  				"kind":       "Shoot",
   147  				"metadata": map[string]interface{}{
   148  					"name": config.ClusterConfig.GardenerConfig.Name,
   149  					"annotations": map[string]interface{}{
   150  						"kcp.provisioner.kyma-project.io/runtime-id": runtimeID,
   151  					},
   152  				},
   153  				"spec": map[string]interface{}{
   154  					"maintenance": map[string]interface{}{
   155  						"timeWindow": map[string]interface{}{
   156  							"begin": "010000+0000",
   157  							"end":   "010000+0000",
   158  						},
   159  					},
   160  				},
   161  			},
   162  		}
   163  		c.gardenerClient.Resource(gardener.ShootResource).Namespace(c.gardenerNamespace).Create(context.Background(), &shoot, v1.CreateOptions{})
   164  	}
   165  
   166  	return schema.OperationStatus{
   167  		RuntimeID: &runtimeID,
   168  		ID:        &operationID,
   169  	}, nil
   170  }
   171  
   172  func (c *FakeClient) DeprovisionRuntime(accountID, runtimeID string) (string, error) {
   173  	c.mu.Lock()
   174  	defer c.mu.Unlock()
   175  
   176  	opId := uuid.New().String()
   177  
   178  	c.operations[opId] = schema.OperationStatus{
   179  		ID:        &opId,
   180  		Operation: schema.OperationTypeDeprovision,
   181  		State:     schema.OperationStateInProgress,
   182  		RuntimeID: &runtimeID,
   183  	}
   184  
   185  	return opId, nil
   186  }
   187  
   188  func (c *FakeClient) ReconnectRuntimeAgent(accountID, runtimeID string) (string, error) {
   189  	return "", fmt.Errorf("not implemented")
   190  }
   191  
   192  func (c *FakeClient) RuntimeOperationStatus(accountID, operationID string) (schema.OperationStatus, error) {
   193  	c.mu.Lock()
   194  	defer c.mu.Unlock()
   195  
   196  	o, found := c.operations[operationID]
   197  	if !found {
   198  		return schema.OperationStatus{}, fmt.Errorf("operation not found")
   199  	}
   200  	return o, nil
   201  }
   202  
   203  func (c *FakeClient) RuntimeStatus(accountID, runtimeID string) (schema.RuntimeStatus, error) {
   204  	c.mu.Lock()
   205  	defer c.mu.Unlock()
   206  
   207  	for _, ops := range c.operations {
   208  		if *ops.RuntimeID == runtimeID {
   209  			return schema.RuntimeStatus{
   210  				RuntimeConfiguration: &schema.RuntimeConfig{
   211  					ClusterConfig: &schema.GardenerConfig{
   212  						Name:   ptr.String("fake-name"),
   213  						Region: ptr.String("fake-region"),
   214  						Seed:   ptr.String("fake-seed"),
   215  					},
   216  					Kubeconfig: ptr.String("kubeconfig-content"),
   217  				},
   218  			}, nil
   219  		}
   220  	}
   221  
   222  	return schema.RuntimeStatus{}, errors.New("no status for given runtime id")
   223  }
   224  
   225  func (c *FakeClient) UpgradeRuntime(accountID, runtimeID string, config schema.UpgradeRuntimeInput) (schema.OperationStatus, error) {
   226  	c.mu.Lock()
   227  	defer c.mu.Unlock()
   228  
   229  	if c.dumpRequest {
   230  		gql, _ := c.graphqlizer.UpgradeRuntimeInputToGraphQL(config)
   231  		fmt.Println(gql)
   232  	}
   233  
   234  	opId := uuid.New().String()
   235  	c.operations[opId] = schema.OperationStatus{
   236  		ID:        &opId,
   237  		RuntimeID: &runtimeID,
   238  		Operation: schema.OperationTypeUpgrade,
   239  		State:     schema.OperationStateInProgress,
   240  	}
   241  	c.upgrades[runtimeID] = config
   242  	return schema.OperationStatus{
   243  		RuntimeID: &runtimeID,
   244  		ID:        &opId,
   245  	}, nil
   246  }
   247  
   248  func (c *FakeClient) UpgradeShoot(accountID, runtimeID string, config schema.UpgradeShootInput) (schema.OperationStatus, error) {
   249  	c.mu.Lock()
   250  	defer c.mu.Unlock()
   251  
   252  	if c.dumpRequest {
   253  		upgradeShootIptGQL, _ := c.graphqlizer.UpgradeShootInputToGraphQL(config)
   254  		fmt.Println(upgradeShootIptGQL)
   255  	}
   256  
   257  	opId := uuid.New().String()
   258  	c.operations[opId] = schema.OperationStatus{
   259  		ID:        &opId,
   260  		RuntimeID: &runtimeID,
   261  		Operation: schema.OperationTypeUpgradeShoot,
   262  		State:     schema.OperationStateInProgress,
   263  	}
   264  	c.shootUpgrades[runtimeID] = config
   265  	return schema.OperationStatus{
   266  		RuntimeID: &runtimeID,
   267  		ID:        &opId,
   268  	}, nil
   269  }
   270  
   271  func (c *FakeClient) IsRuntimeUpgraded(runtimeID string, version string) bool {
   272  	input, found := c.upgrades[runtimeID]
   273  	if found && version != "" && input.KymaConfig != nil {
   274  		return input.KymaConfig.Version == version
   275  	}
   276  
   277  	return found
   278  }
   279  
   280  func (c *FakeClient) IsShootUpgraded(runtimeID string) bool {
   281  	_, found := c.shootUpgrades[runtimeID]
   282  	return found
   283  }
   284  
   285  func (c *FakeClient) LastShootUpgrade(runtimeID string) (schema.UpgradeShootInput, bool) {
   286  	input, found := c.shootUpgrades[runtimeID]
   287  	return input, found
   288  }
   289  
   290  func (c *FakeClient) LastProvisioning() schema.ProvisionRuntimeInput {
   291  	r := c.runtimes[len(c.runtimes)-1]
   292  	return r.runtimeInput
   293  }