Skip to content

Commit

Permalink
Adding logic to check fixpacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoaeb Jindani committed Apr 9, 2024
1 parent 800c2af commit ecc9ecf
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
105 changes: 104 additions & 1 deletion pkg/command/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ import (
"fmt"
"os"
"runtime"
"strings"
"time"

"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/go-logr/zapr"
routev1 "github.com/openshift/api/route/v1"
"github.com/operator-framework/operator-lib/leader"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/pkg/errors"
Expand All @@ -42,16 +49,19 @@ import (
apis "github.com/IBM-Blockchain/fabric-operator/api"
ibpv1beta1 "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
controller "github.com/IBM-Blockchain/fabric-operator/controllers"
"github.com/IBM-Blockchain/fabric-operator/defaultconfig/console"
oconfig "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
"github.com/IBM-Blockchain/fabric-operator/pkg/migrator"
"github.com/IBM-Blockchain/fabric-operator/pkg/offering"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
openshiftv1 "github.com/openshift/api/config/v1"

"k8s.io/apimachinery/pkg/types"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client"

uberzap "go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
Expand Down Expand Up @@ -169,7 +179,7 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte
"Enabling this will ensure there is only one active controller manager.")
}
flag.Parse()

config := ctrl.GetConfigOrDie()
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Expand All @@ -186,6 +196,10 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte

log.Info("Registering Components.")

//This Method Checks if MustgatherTag in ibm-hlfsupport-deployer configmap is same as the console tag in the operator
// binary (if it is not same, it delete the configmaps ibm-hlfsupport-console-deployer and ibm-hlfsupport-console-console)
CheckForFixPacks(config, operatorNamespace)

// Setup Scheme for all resources
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
log.Error(err, "")
Expand Down Expand Up @@ -312,3 +326,92 @@ func GetOperatorNamespace() (string, error) {

return operatorNamespace, nil
}
func CheckForFixPacks(config *rest.Config, operatornamespace string) {
clientset, err := kubernetes.NewForConfig(config)

// Create a dynamic client
dynamicClient := dynamic.NewForConfigOrDie(config)

// Define your custom resource type
//customResourceName := "ibpconsoles"
customResourceNamespace := "ibmsupport"
gvr := schema.GroupVersionResource{
Group: "ibp.com",
Version: "v1beta1",
Resource: "ibpconsoles",
}

// Retrieve the list of objects in your custom resource
list, err := dynamicClient.Resource(gvr).Namespace(customResourceNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}

var consoleObjectName string
// Print the names of all objects
for _, obj := range list.Items {

consoleObjectName = obj.GetName()
// If you want to do something with the object, you can access it here

}
log.Info(fmt.Sprintf("Latest Console Tag is %s", console.GetImages().ConsoleTag))
m, err := util.GetConfigMap(clientset, operatornamespace, "ibm-hlfsupport-console-deployer")
data := m.Data
yamlstring := data["settings.yaml"]
var datamap map[string]interface{}
err = yaml.Unmarshal([]byte(yamlstring), &datamap)
if err != nil {
panic(err)
}
mustgathertag := ""
if otherimages, ok := datamap["otherImages"].(map[interface{}]interface{}); ok {
if mustgathertag, ok = otherimages["mustgatherTag"].(string); ok {
log.Info(fmt.Sprintf("Value of Mustgather tag is %s:", mustgathertag))
} else {
log.Info(fmt.Sprintf("Field C is not a string"))
}
}

//if the latest console tag and the mustgather tag are not same, then we will delete the below two configmaps
if console.GetImages().ConsoleTag != mustgathertag {
log.Info(fmt.Sprintf("Console Tag is %s and Mustgater tag is %s", console.GetImages().ConsoleTag, mustgathertag))

// set the webhook image here as well
// Specify deployment namespace and name
namespace := "ibm-hlfsupport-infra"
deploymentName := "ibm-hlfsupport-webhook"

// Retrieve the deployment
deployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, v1.GetOptions{})
if err != nil {
panic(err.Error())
}
existingwebhookimage := strings.Split(deployment.Spec.Template.Spec.Containers[0].Image, ":")[0]
existingwebhookimage = existingwebhookimage + ":" + console.GetImages().ConsoleTag

deployment.Spec.Template.Spec.Containers[0].Image = existingwebhookimage

// Update the deployment
_, err = clientset.AppsV1().Deployments(namespace).Update(context.TODO(), deployment, v1.UpdateOptions{})
if err != nil {
panic(err.Error())
}

util.DeleteConfigMapIfExists(clientset, operatornamespace, consoleObjectName+"-console")
util.DeleteConfigMapIfExists(clientset, operatornamespace, consoleObjectName+"-deployer")

} else {
log.Info("Looks like the operator was restarted...")
}

}

func containsString(slice []metav1.GroupVersionForDiscovery, s string) bool {
for _, item := range slice {
if item.GroupVersion == s {
return true
}
}
return false
}
21 changes: 21 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"go.uber.org/zap/zapcore"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -54,6 +55,7 @@ import (
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
yaml1 "sigs.k8s.io/yaml"
Expand Down Expand Up @@ -965,3 +967,22 @@ func SetupLogging(loglevel string) (*uberzap.Logger, error) {

return logger, nil
}
func GetConfigMap(clientset *kubernetes.Clientset, namespace, configMapName string) (*v1.ConfigMap, error) {
configMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
if err != nil {
return nil, err
}
return configMap, nil
}
func DeleteConfigMapIfExists(clientset *kubernetes.Clientset, namespace, configMapName string) error {
_, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
if err != nil {
return nil
}

err = clientset.CoreV1().ConfigMaps(namespace).Delete(context.TODO(), configMapName, metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}

0 comments on commit ecc9ecf

Please sign in to comment.