Skip to content

Commit

Permalink
Added storing graphQL query
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Jul 30, 2024
1 parent 7a4cbf1 commit 67dc375
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 82 deletions.
25 changes: 18 additions & 7 deletions components/provisioner/internal/api/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"fmt"
"github.com/kyma-project/control-plane/components/provisioner/internal/util"

"github.com/kyma-project/control-plane/components/provisioner/internal/api/middlewares"
"github.com/pkg/errors"
Expand All @@ -14,9 +15,10 @@ import (
)

type Resolver struct {
provisioning provisioning.Service
validator Validator
tenantUpdater TenantUpdater
provisioning provisioning.Service
validator Validator
tenantUpdater TenantUpdater
enableDumpShootSpec bool
}

func (r *Resolver) Mutation() gqlschema.MutationResolver {
Expand All @@ -34,11 +36,12 @@ func (r *Resolver) Query() gqlschema.QueryResolver {
}
}

func NewResolver(provisioningService provisioning.Service, validator Validator, tenantUpdater TenantUpdater) *Resolver {
func NewResolver(provisioningService provisioning.Service, validator Validator, tenantUpdater TenantUpdater, enableDumpShootSpec bool) *Resolver {
return &Resolver{
provisioning: provisioningService,
validator: validator,
tenantUpdater: tenantUpdater,
provisioning: provisioningService,
validator: validator,
tenantUpdater: tenantUpdater,
enableDumpShootSpec: enableDumpShootSpec,
}
}

Expand All @@ -59,6 +62,14 @@ func (r *Resolver) ProvisionRuntime(ctx context.Context, config gqlschema.Provis

log.Infof("Requested provisioning of Runtime %s.", config.RuntimeInput.Name)

if r.enableDumpShootSpec {
err := util.PersistGraphQL(fmt.Sprintf("/tmp/%s-mutation.graphql.json", config.ClusterConfig.GardenerConfig.Name), config)
if err != nil {
log.Errorf("Failed to dump GraphQL mutation for Runtime %s: %s", config.RuntimeInput.Name, err)
return nil, err
}
}

operationStatus, err := r.provisioning.ProvisionRuntime(config, tenant, subAccount)
if err != nil {
log.Errorf("Failed to provision Runtime %s: %s", config.RuntimeInput.Name, err)
Expand Down
38 changes: 1 addition & 37 deletions components/provisioner/internal/gardener/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"time"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/kyma-project/control-plane/components/provisioner/internal/apperrors"
"github.com/kyma-project/control-plane/components/provisioner/internal/util"
"github.com/mitchellh/mapstructure"
Expand All @@ -24,8 +22,6 @@ import (
"github.com/kyma-project/control-plane/components/provisioner/internal/provisioning/persistence/dbsession"
log "github.com/sirupsen/logrus"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"

"sigs.k8s.io/yaml"
)

//go:generate mockery --name=Client
Expand Down Expand Up @@ -67,13 +63,6 @@ func (g *GardenerProvisioner) ProvisionCluster(cluster model.Cluster, operationI
return err.Append("failed to convert cluster config to Shoot template")
}

if g.enableDumpShootSpec {
errs := util.WriteToPV(shootTemplate.DeepCopy())
if errs != nil {
log.Errorf("Error writing Shoot to PV: %s", errs.Error())
}
}

region := cluster.ClusterConfig.Region

if region == "me-central2" {
Expand Down Expand Up @@ -107,7 +96,7 @@ func (g *GardenerProvisioner) ProvisionCluster(cluster model.Cluster, operationI

if g.enableDumpShootSpec {
path := fmt.Sprintf("%s/%s-%s.yaml", "/testdata/provisioner", shootTemplate.Namespace, shootTemplate.Name)
if err := persist(path, shootTemplate); err != nil {
if err := util.PersistShoot(path, shootTemplate); err != nil {
log.Errorf("Error marshaling Shoot spec: %s", err.Error())
}
log.Infof("Shoot spec dumped to %s", path)
Expand All @@ -124,31 +113,6 @@ func (g *GardenerProvisioner) ProvisionCluster(cluster model.Cluster, operationI
return nil
}

var getWriter = func(filePath string) (io.Writer, error) {
file, err := os.Create(filePath)
if err != nil {
return nil, fmt.Errorf("unable to create file: %w", err)
}
return file, nil
}

func persist(path string, s *gardener.Shoot) error {
writer, err := getWriter(path)
if err != nil {
return fmt.Errorf("unable to create file: %w", err)
}

b, err := yaml.Marshal(s)
if err != nil {
return fmt.Errorf("unable to marshal shoot: %w", err)
}

if _, err = writer.Write(b); err != nil {
return fmt.Errorf("unable to write to file: %w", err)
}
return nil
}

func (g *GardenerProvisioner) UpgradeCluster(clusterID string, upgradeConfig model.GardenerConfig) apperrors.AppError {
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
shoot, err := g.shootClient.Get(context.Background(), upgradeConfig.Name, v1.GetOptions{})
Expand Down
38 changes: 0 additions & 38 deletions components/provisioner/internal/util/pv_writer.go

This file was deleted.

53 changes: 53 additions & 0 deletions components/provisioner/internal/util/shoot_spec_dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package util

import (
"encoding/json"
"fmt"
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
"io"
"os"
"sigs.k8s.io/yaml"
)

func PersistShoot(path string, shoot *v1beta1.Shoot) error {
writer, err := getWriter(path)
if err != nil {
return fmt.Errorf("unable to create file: %w", err)
}

b, err := yaml.Marshal(shoot)
if err != nil {
return fmt.Errorf("unable to marshal shoot: %w", err)
}

if _, err = writer.Write(b); err != nil {
return fmt.Errorf("unable to write to file: %w", err)
}
return nil
}

func getWriter(filePath string) (io.Writer, error) {
file, err := os.Create(filePath)
if err != nil {
return nil, fmt.Errorf("unable to create file: %w", err)
}
return file, nil
}

func PersistGraphQL(path string, mutation gqlschema.ProvisionRuntimeInput) error {
writer, err := getWriter(path)
if err != nil {
return fmt.Errorf("unable to create file: %w", err)
}

b, err := json.Marshal(mutation)
if err != nil {
return fmt.Errorf("unable to marshal GraphQL mutation: %w", err)
}

if _, err = writer.Write(b); err != nil {
return fmt.Errorf("unable to write to file: %w", err)
}
return nil
}

0 comments on commit 67dc375

Please sign in to comment.