Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NO-JIRA: HyperShift code linter PR837 followup #910

Merged
merged 1 commit into from Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions pkg/operator/controller.go
Expand Up @@ -13,6 +13,7 @@ import (
kmeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
kubeinformers "k8s.io/client-go/informers"
corev1informers "k8s.io/client-go/informers/core/v1"
Expand Down Expand Up @@ -42,6 +43,7 @@ import (
"github.com/openshift/cluster-node-tuning-operator/pkg/util"
"github.com/openshift/cluster-node-tuning-operator/version"

mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
mcfgclientset "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
mcfginformers "github.com/openshift/machine-config-operator/pkg/generated/informers/externalversions"
)
Expand Down Expand Up @@ -79,6 +81,8 @@ type Controller struct {

pc *ProfileCalculator

scheme *runtime.Scheme // used by the HyperShift code

// bootcmdlineConflict is the internal operator's cache of Profiles
// tracked as having kernel command-line conflict due to belonging
// to the same MCP.
Expand All @@ -100,12 +104,17 @@ func NewController() (*Controller, error) {

listers := &ntoclient.Listers{}
clients := &ntoclient.Clients{}
scheme := runtime.NewScheme()
if err := mcfgv1.Install(scheme); err != nil {
return nil, err
}
controller := &Controller{
kubeconfig: kubeconfig,
workqueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
listers: listers,
clients: clients,
pc: NewProfileCalculator(listers, clients),
scheme: scheme,
}

controller.bootcmdlineConflict = map[string]bool{}
Expand Down Expand Up @@ -920,7 +929,7 @@ func (c *Controller) syncMachineConfigHyperShift(nodePoolName string, profile *t
mc := NewMachineConfig(mcName, annotations, nil, kernelArguments)

// put the MC into a ConfigMap and create that instead
mcConfigMap, err = newConfigMapForMachineConfig(configMapName, nodePoolName, mc)
mcConfigMap, err = c.newConfigMapForMachineConfig(configMapName, nodePoolName, mc)
if err != nil {
klog.Errorf("failed to generate ConfigMap %s for MachineConfig %s: %v", configMapName, mc.ObjectMeta.Name, err)
return nil
Expand All @@ -937,7 +946,7 @@ func (c *Controller) syncMachineConfigHyperShift(nodePoolName string, profile *t

// A ConfigMap with the same name was found
// we need to make sure the contents are up-to-date.
mc, err := getMachineConfigFromConfigMap(mcConfigMap)
mc, err := c.getMachineConfigFromConfigMap(mcConfigMap)
if err != nil {
klog.Errorf("failed to get MachineConfig from ConfigMap %s: %v", mcConfigMap.Name, err)
return nil
Expand Down Expand Up @@ -971,7 +980,7 @@ func (c *Controller) syncMachineConfigHyperShift(nodePoolName string, profile *t
l := MachineConfigGenerationLogLine(!kernelArgsEq, bootcmdline)
klog.V(2).Infof("syncMachineConfigHyperShift(): updating MachineConfig %s with%s", mc.ObjectMeta.Name, l)

newData, err := serializeMachineConfig(mc)
newData, err := c.serializeMachineConfig(mc)
if err != nil {
klog.Errorf("failed to serialize ConfigMap for MachineConfig %s: %v", mc.Name, err)
return nil
Expand Down
23 changes: 6 additions & 17 deletions pkg/operator/hypershift.go
Expand Up @@ -17,7 +17,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -238,14 +237,9 @@ func parseNamespacedName(namespacedName string) string {
return parts[0]
}

func getMachineConfigFromConfigMap(config *corev1.ConfigMap) (*mcfgv1.MachineConfig, error) {
scheme := runtime.NewScheme()
if err := mcfgv1.Install(scheme); err != nil {
return nil, err
}

func (c *Controller) getMachineConfigFromConfigMap(config *corev1.ConfigMap) (*mcfgv1.MachineConfig, error) {
YamlSerializer := serializer.NewSerializerWithOptions(
serializer.DefaultMetaFactory, scheme, scheme,
serializer.DefaultMetaFactory, c.scheme, c.scheme,
serializer.SerializerOptions{Yaml: true, Pretty: true, Strict: true},
)

Expand All @@ -262,8 +256,8 @@ func getMachineConfigFromConfigMap(config *corev1.ConfigMap) (*mcfgv1.MachineCon
return mcObj, nil
}

func newConfigMapForMachineConfig(configMapName string, nodePoolName string, mc *mcfgv1.MachineConfig) (*corev1.ConfigMap, error) {
mcManifest, err := serializeMachineConfig(mc)
func (c *Controller) newConfigMapForMachineConfig(configMapName string, nodePoolName string, mc *mcfgv1.MachineConfig) (*corev1.ConfigMap, error) {
mcManifest, err := c.serializeMachineConfig(mc)
if err != nil {
return nil, fmt.Errorf("failed to serialize ConfigMap for MachineConfig %s: %v", mc.Name, err)
}
Expand All @@ -289,14 +283,9 @@ func newConfigMapForMachineConfig(configMapName string, nodePoolName string, mc
return ret, nil
}

func serializeMachineConfig(mc *mcfgv1.MachineConfig) ([]byte, error) {
scheme := runtime.NewScheme()
if err := mcfgv1.Install(scheme); err != nil {
return nil, err
}

func (c *Controller) serializeMachineConfig(mc *mcfgv1.MachineConfig) ([]byte, error) {
YamlSerializer := serializer.NewSerializerWithOptions(
serializer.DefaultMetaFactory, scheme, scheme,
serializer.DefaultMetaFactory, c.scheme, c.scheme,
serializer.SerializerOptions{Yaml: true, Pretty: true, Strict: true},
)
buff := bytes.Buffer{}
Expand Down