Skip to content

Commit 9b5f85d

Browse files
author
YuChen
committed
revert the host back to default value
Signed-off-by: YuChen <[email protected]>
1 parent c65b695 commit 9b5f85d

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

controllers/operandrequest/reconcile_operand.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,16 +948,48 @@ func (r *Reconciler) createK8sResource(ctx context.Context, k8sResTemplate unstr
948948
name := k8sResTemplate.GetName()
949949
namespace := k8sResTemplate.GetNamespace()
950950

951+
if kind == "Route" {
952+
host, _, err := unstructured.NestedString(k8sResTemplate.Object, "spec", "host")
953+
klog.Info("11111 host %v: ", host)
954+
955+
if err != nil {
956+
klog.Errorf("failed to get spec.host for Route: %v", err)
957+
return err
958+
}
959+
960+
// Calculate and set hash for spec.host
961+
hostHash := util.CalculateHash(host)
962+
if newAnnotations == nil {
963+
newAnnotations = make(map[string]string)
964+
}
965+
newAnnotations["openshift.io/keycloak-host.hashedData"] = hostHash
966+
// klog.Infof("22222 Set host hash annotation: %s", hostHash)
967+
}
968+
951969
if k8sResConfig != nil {
952970
k8sResConfigDecoded := make(map[string]interface{})
953971
k8sResConfigUnmarshalErr := json.Unmarshal(k8sResConfig.Raw, &k8sResConfigDecoded)
954972
if k8sResConfigUnmarshalErr != nil {
955973
klog.Errorf("failed to unmarshal k8s Resource Config: %v", k8sResConfigUnmarshalErr)
956974
}
957-
975+
klog.Infof("11111 k8sResConfig: %v", k8sResConfigDecoded)
958976
for k, v := range k8sResConfigDecoded {
959977
k8sResTemplate.Object[k] = v
960978
}
979+
980+
if kind == "Route" {
981+
if host, found := k8sResConfigDecoded["spec"].(map[string]interface{})["host"].(string); found {
982+
hostHash := util.CalculateHash(host)
983+
984+
if newAnnotations == nil {
985+
newAnnotations = make(map[string]string)
986+
}
987+
newAnnotations["openshift.io/keycloak-host.hashedData"] = hostHash
988+
klog.Infof(" 2222 Route spec.host: %s, calculated host-hash: %s", host, hostHash)
989+
} else {
990+
klog.Warningf("spec.host not found in k8sResConfig for Route")
991+
}
992+
}
961993
}
962994

963995
r.EnsureLabel(k8sResTemplate, map[string]string{constant.OpreqLabel: "true"})
@@ -983,6 +1015,7 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
9831015
apiversion := existingK8sRes.GetAPIVersion()
9841016
name := existingK8sRes.GetName()
9851017
namespace := existingK8sRes.GetNamespace()
1018+
9861019
if kind == "Job" {
9871020
existingK8sRes := unstructured.Unstructured{
9881021
Object: map[string]interface{}{
@@ -1038,6 +1071,63 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
10381071
return nil
10391072
}
10401073

1074+
if kind == "Route" {
1075+
existingAnnos := existingK8sRes.GetAnnotations()
1076+
existingHostHash := existingAnnos["openshift.io/keycloak-host.hashedData"]
1077+
klog.Infof("111--11 existing host hash: %s", existingHostHash)
1078+
1079+
if k8sResConfig != nil {
1080+
k8sResConfigDecoded := make(map[string]interface{})
1081+
k8sResConfigUnmarshalErr := json.Unmarshal(k8sResConfig.Raw, &k8sResConfigDecoded)
1082+
if k8sResConfigUnmarshalErr != nil {
1083+
klog.Errorf("failed to unmarshal k8s Resource Config: %v", k8sResConfigUnmarshalErr)
1084+
}
1085+
klog.Infof("111--11 k8sResConfig: %v", k8sResConfigDecoded)
1086+
1087+
// Read the host from the OperandConfig
1088+
if newHost, found := k8sResConfigDecoded["spec"].(map[string]interface{})["host"].(string); found {
1089+
newHostHash := util.CalculateHash(newHost)
1090+
1091+
klog.Infof("3333 new host hash: %s", newHostHash)
1092+
1093+
// Only re-create the route if the custom host has been removed
1094+
if newHost == "" && existingHostHash != newHostHash {
1095+
klog.Infof("4444 Custom host removed or changed, resetting to default value")
1096+
1097+
// create a new template of k8s resource
1098+
var templatek8sRes unstructured.Unstructured
1099+
templatek8sRes.SetAPIVersion(apiversion)
1100+
templatek8sRes.SetKind(kind)
1101+
templatek8sRes.SetName(name)
1102+
templatek8sRes.SetNamespace(namespace)
1103+
1104+
if err := r.deleteK8sResource(ctx, existingK8sRes, namespace); err != nil {
1105+
return errors.Wrap(err, "failed to delete Route for recreation")
1106+
}
1107+
if err := r.createK8sResource(ctx, templatek8sRes, k8sResConfig, newLabels, newAnnotations, ownerReferences); err != nil {
1108+
return errors.Wrap(err, "failed to update k8s resource")
1109+
}
1110+
return nil
1111+
}
1112+
1113+
// Only update the resource if the host has changed
1114+
if existingHostHash != newHostHash {
1115+
klog.Infof("5555 Custom host changed, updating Route with new host hash")
1116+
1117+
existingAnnos["openshift.io/keycloak-host.hashedData"] = newHostHash
1118+
r.EnsureAnnotation(existingK8sRes, existingAnnos)
1119+
}
1120+
// Update resource in the cluster
1121+
if err := r.Update(ctx, &existingK8sRes); err != nil {
1122+
return errors.Wrap(err, "failed to update Route resource with new host hash")
1123+
}
1124+
klog.Infof("Updated Route with new host hash: %s", newHostHash)
1125+
1126+
}
1127+
}
1128+
return nil
1129+
}
1130+
10411131
// Update the k8s res
10421132
err := wait.PollImmediate(constant.DefaultCRFetchPeriod, constant.DefaultCRFetchTimeout, func() (bool, error) {
10431133

controllers/util/util.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package util
1818

1919
import (
2020
"bytes"
21+
"crypto/sha256"
22+
"encoding/hex"
2123
"fmt"
2224
"os"
2325
"sort"
@@ -150,6 +152,14 @@ func StringSliceContentEqual(a, b []string) bool {
150152
return true
151153
}
152154

155+
func CalculateHash(input string) string {
156+
if input == "" {
157+
return ""
158+
}
159+
hashedData := sha256.Sum256([]byte(input))
160+
return hex.EncodeToString(hashedData[:7])
161+
}
162+
153163
// WaitTimeout waits for the waitgroup for the specified max timeout.
154164
// Returns true if waiting timed out.
155165
func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {

0 commit comments

Comments
 (0)