Skip to content

Commit

Permalink
feat: handle panic exceptions about istio objects (#14)
Browse files Browse the repository at this point in the history
* feat: handle panic exceptions about istio objects

* feat: lint
  • Loading branch information
Tchoupinax authored Jul 21, 2024
1 parent 971a66f commit 5d74dcc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
46 changes: 29 additions & 17 deletions steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ func MigrationWorkflow(

utils.LogInfo("2.1 Updating Istio destination rules")
var temporalDestinationRule = currentDestinationRule
delete(temporalDestinationRule.Spec.Subsets[0].Labels, changingLabelKey)
_, err = istioClient.NetworkingV1alpha3().DestinationRules(namespace).Update(context.TODO(), temporalDestinationRule, metav1.UpdateOptions{})
utils.Check(err)
utils.LogSuccess("2.1 Istio destination rules updated")
if len(temporalDestinationRule.Spec.Subsets) > 0 {
delete(temporalDestinationRule.Spec.Subsets[0].Labels, changingLabelKey)
_, err = istioClient.NetworkingV1alpha3().DestinationRules(namespace).Update(context.TODO(), temporalDestinationRule, metav1.UpdateOptions{})
utils.Check(err)
utils.LogSuccess("2.1 Istio destination rules updated")
} else {
utils.LogSuccess("2.1 No Istio destination rules found")
}

utils.LogInfo("2.2 Keda")
keda.PauseScaledObject(crdClient, clientset, deploymentName, namespace)
Expand Down Expand Up @@ -178,21 +182,29 @@ func AddLabelToIstioDestinatonRulesSelector(
utils.LogInfo("====== Additionnal step ====================================")
utils.LogInfo("9. Add the label as a selector in istio destination rules...")
currentDestinationRule, err := istioClient.NetworkingV1alpha3().DestinationRules(namespace).Get(context.TODO(), applicationName, v1.GetOptions{})
utils.Check(err)

var futureDestinationRule = currentDestinationRule
if removeLabel {
// Update the value of the label
delete(futureDestinationRule.Spec.Subsets[0].Labels, changingLabelKey)
if strings.HasSuffix(err.Error(), "not found") {
utils.LogSuccess("9. Not Istio rules configured")
utils.LogInfo("============================================================")
} else {
futureDestinationRule.Spec.Subsets[0].Labels[changingLabelKey] = changingLabelValue
// If the string is empty, remove the label (see deployment)
}
fmt.Println(currentDestinationRule)
fmt.Println(err)
utils.Check(err)

var futureDestinationRule = currentDestinationRule
if removeLabel {
// Update the value of the label
delete(futureDestinationRule.Spec.Subsets[0].Labels, changingLabelKey)
} else {
futureDestinationRule.Spec.Subsets[0].Labels[changingLabelKey] = changingLabelValue
// If the string is empty, remove the label (see deployment)
}

// Update the service in the cluster
_, updateDestinationRuleError := istioClient.NetworkingV1alpha3().DestinationRules(namespace).Update(context.TODO(), futureDestinationRule, metav1.UpdateOptions{})
utils.Check(updateDestinationRuleError)
// Update the service in the cluster
_, updateDestinationRuleError := istioClient.NetworkingV1alpha3().DestinationRules(namespace).Update(context.TODO(), futureDestinationRule, metav1.UpdateOptions{})
utils.Check(updateDestinationRuleError)

utils.LogSuccess("9. Istio destination rules configured")
utils.LogInfo("============================================================")
utils.LogSuccess("9. Istio destination rules configured")
utils.LogInfo("============================================================")
}
}
31 changes: 15 additions & 16 deletions summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,45 +79,44 @@ func resourcesAnalyze(
namespace,
podLabels,
)
for _, a := range services {
resources = append(resources, a)
}

resources = append(resources, services...)

// Native HPA
hpa := native.NativeHPAResourceAnalyze(
clientset,
namespace,
deploymentName,
)
for _, a := range hpa {
resources = append(resources, a)
}

resources = append(resources, hpa...)

// Istio Authorization Policies
authorizationPolicies := authorizationPolicy.IstioAuthorizationPolicyResourceAnalyze(
istioClient,
namespace,
podLabels,
)
for _, a := range authorizationPolicies {
resources = append(resources, a)
}

resources = append(resources, authorizationPolicies...)

// Destination rules
destinationRules := authorizationPolicy.IstioDestinationRuleResourceAnalyze(
istioClient,
namespace,
podLabels,
)
for _, a := range destinationRules {
resources = append(resources, a)
}

resources = append(resources, destinationRules...)

// Keda — ScaledObject
scaledobjects := keda.KedaScaledObjectResourceAnalyze(
crdClient,
namespace,
deploymentName,
)
for _, a := range scaledobjects {
resources = append(resources, a)
}

resources = append(resources, scaledobjects...)

if len(podLabels) == 1 && podLabels[changingLabelKey] != "" {
utils.LogError(fmt.Sprintf("The label \"%s\" can not be edited because it's the only one in the matching set for the deployment", changingLabelKey))
Expand All @@ -129,7 +128,7 @@ func resourcesAnalyze(
os.Exit(1)
}

utils.LogSuccess("Resources analyzed (" + strconv.FormatFloat(time.Now().Sub(startTime).Seconds(), 'f', 2, 64) + "s)")
utils.LogSuccess("Resources analyzed (" + strconv.FormatFloat(time.Since(startTime).Seconds(), 'f', 2, 64) + "s)")

webapp.StartWebServer(
deploymentName,
Expand Down
17 changes: 14 additions & 3 deletions summary/webapp/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package webapp

import (
"fmt"
"net/http"
"strings"
"text/template"
Expand Down Expand Up @@ -53,10 +54,20 @@ func StartWebServer(
NativeResources: filter(resources, func(r resource.Resource) bool { return r.Category == "Native" }),
PodLabels: podLabels,
}
ViewRenderer.Template(w, http.StatusOK, tpls, data)
err := ViewRenderer.Template(w, http.StatusOK, tpls, data)
utils.Check(err)
})

utils.LogInfo("View resumed here: http://localhost:8080")
utils.OpenURL("http://localhost:8080")
go http.ListenAndServe(":8080", nil)
err := utils.OpenURL("http://localhost:8080")
utils.Check(err)

httpServerError := make(chan error, 1)
go func() {
httpServerError <- http.ListenAndServe(":8080", nil)
}()

if err := <-httpServerError; err != nil {
fmt.Println(err)
}
}

0 comments on commit 5d74dcc

Please sign in to comment.