Skip to content

Commit

Permalink
before test implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslaw-pieszka committed Aug 29, 2024
1 parent 7bd111c commit f997c61
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 264 deletions.
5 changes: 2 additions & 3 deletions common/runtime/model.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package runtime

import (
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"time"

"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
Expand Down Expand Up @@ -54,7 +53,7 @@ type RuntimeDTO struct {
AVSInternalEvaluationID int64 `json:"avsInternalEvaluationID"`
KymaConfig *gqlschema.KymaConfigInput `json:"kymaConfig,omitempty"`
ClusterConfig *gqlschema.GardenerConfigInput `json:"clusterConfig,omitempty"`
RuntimeResource *imv1.Runtime `json:"runtimeResource,omitempty"`
RuntimeResource *map[string]interface{} `json:"runtimeResource,omitempty"`
}

type RuntimeStatus struct {
Expand Down Expand Up @@ -121,7 +120,7 @@ const (
ClusterConfigParam = "cluster_config"
ExpiredParam = "expired"
GardenerConfigParam = "gardener_config"
RuntimeResourceParam = "runtime_resource"
RuntimeConfigParam = "runtime_config"
)

type OperationDetail string
Expand Down
55 changes: 42 additions & 13 deletions internal/runtime/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package runtime
import (
"context"
"fmt"

Check failure on line 5 in internal/runtime/handler.go

View workflow job for this annotation

GitHub Actions / run-go-linter

File is not `goimports`-ed (goimports)
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"github.com/kyma-project/kyma-environment-broker/internal/process/steps"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"net/http"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -187,7 +188,7 @@ func (h *Handler) getRuntimes(w http.ResponseWriter, req *http.Request) {
kymaConfig := getBoolParam(pkg.KymaConfigParam, req)
clusterConfig := getBoolParam(pkg.ClusterConfigParam, req)
gardenerConfig := getBoolParam(pkg.GardenerConfigParam, req)
runtimeResourceConfig := getBoolParam(pkg.RuntimeResourceParam, req)
runtimeResourceConfig := getBoolParam(pkg.RuntimeConfigParam, req)

instances, count, totalCount, err := h.listInstances(filter)
if err != nil {
Expand Down Expand Up @@ -216,25 +217,30 @@ func (h *Handler) getRuntimes(w http.ResponseWriter, req *http.Request) {
httputil.WriteErrorResponse(w, http.StatusInternalServerError, err)
return
}
err = h.setRuntimeOptionalAttributes(&dto, kymaConfig, clusterConfig, gardenerConfig)

instanceDrivenByKimOnly := h.kimConfig.IsDrivenByKimOnly(dto.ServicePlanName)

err = h.setRuntimeOptionalAttributes(&dto, kymaConfig, clusterConfig, gardenerConfig, instanceDrivenByKimOnly)
if err != nil {
h.logger.Warn(fmt.Sprintf("unable to set optional attributes: %s", err.Error()))
httputil.WriteErrorResponse(w, http.StatusInternalServerError, err)
return
}

if runtimeResourceConfig { //TODO handle suspension scenario
instanceDrivenByKimOnly := h.kimConfig.IsDrivenByKimOnly(dto.ServicePlanName)
runtime := imv1.Runtime{}
if runtimeResourceConfig && dto.RuntimeID != "" {
runtimeResourceName, runtimeNamespaceName := h.getRuntimeNamesFromLastOperation(dto)

if instanceDrivenByKimOnly {
err := h.k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: "kcp-system", //TODO get namespace from the last operation
Name: steps.KymaRuntimeResourceNameFromID(dto.RuntimeID), //TODO should get runtimeID from the last operation as well
}, &runtime)
runtimeResourceObject := &unstructured.Unstructured{}
runtimeResourceObject.SetGroupVersionKind(RuntimeResourceGVK())
err = h.k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: runtimeNamespaceName,
Name: runtimeResourceName,
}, runtimeResourceObject)
if err != nil {
h.logger.Warn(fmt.Sprintf("unable to get Runtime resource %s/%s: %s", dto.InstanceID, dto.RuntimeID, err.Error()))
}
dto.RuntimeResource = &runtime
dto.RuntimeResource = &runtimeResourceObject.Object
}
}

Expand All @@ -249,6 +255,20 @@ func (h *Handler) getRuntimes(w http.ResponseWriter, req *http.Request) {
httputil.WriteResponse(w, http.StatusOK, runtimePage)
}

func (h *Handler) getRuntimeNamesFromLastOperation(dto pkg.RuntimeDTO) (string, string) {
// TODO get rid of additional DB query - we have this info fetched from DB but it is tedious to pass it through
op, err := h.operationsDb.GetLastOperation(dto.InstanceID)
runtimeResourceName := steps.KymaRuntimeResourceNameFromID(dto.RuntimeID)
runtimeNamespaceName := "kcp-system"
if err != nil || op.RuntimeResourceName == "" {
runtimeResourceName = op.RuntimeResourceName
}
if err != nil || op.KymaResourceNamespace == "" {
runtimeNamespaceName = op.KymaResourceNamespace
}
return runtimeResourceName, runtimeNamespaceName
}

func (h *Handler) takeLastNonDryRunClusterOperations(oprs []internal.UpgradeClusterOperation) ([]internal.UpgradeClusterOperation, int) {
toReturn := make([]internal.UpgradeClusterOperation, 0)
totalCount := 0
Expand Down Expand Up @@ -379,7 +399,8 @@ func (h *Handler) setRuntimeLastOperation(dto *pkg.RuntimeDTO) error {
return nil
}

func (h *Handler) setRuntimeOptionalAttributes(dto *pkg.RuntimeDTO, kymaConfig, clusterConfig, gardenerConfig bool) error {
func (h *Handler) setRuntimeOptionalAttributes(dto *pkg.RuntimeDTO, kymaConfig, clusterConfig, gardenerConfig, drivenByKimOnly bool) error {

if kymaConfig || clusterConfig {
states, err := h.runtimeStatesDb.ListByRuntimeID(dto.RuntimeID)
if err != nil && !dberr.IsNotFound(err) {
Expand All @@ -400,7 +421,7 @@ func (h *Handler) setRuntimeOptionalAttributes(dto *pkg.RuntimeDTO, kymaConfig,
}
}

if gardenerConfig {
if gardenerConfig && dto.RuntimeID != "" && !drivenByKimOnly {
runtimeStatus, err := h.provisionerClient.RuntimeStatus(dto.GlobalAccountID, dto.RuntimeID)
if err != nil {
dto.Status.GardenerConfig = nil
Expand Down Expand Up @@ -494,3 +515,11 @@ func getBoolParam(param string, req *http.Request) bool {

return requested
}

func RuntimeResourceGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: "infrastructuremanager.kyma-project.io",
Version: "v1",
Kind: "Runtime",
}
}
Loading

0 comments on commit f997c61

Please sign in to comment.