Skip to content

Commit

Permalink
Add serviceport var to CRD
Browse files Browse the repository at this point in the history
This patch allows users to modify the nfd-master Service port via CRD
with the variable Operand.ServicePort , will deafult to 12000 when not
defined

Signed-off-by: Carlos Eduardo Arango Gutierrez <carangog@redhat.com>
  • Loading branch information
ArangoGutierrez committed Apr 16, 2021
1 parent 8ce01e4 commit 565899e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
13 changes: 12 additions & 1 deletion api/v1/nodefeaturediscovery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,26 @@ type NodeFeatureDiscoverySpec struct {

// OperandSpec describes configuration options for the operand
type OperandSpec struct {
// Namespace defines the namespace to deploy nfd-master
// and nfd-worker pods
// +kubebuilder:validation:Pattern=[a-zA-Z0-9\.\-\/]+
Namespace string `json:"namespace,omitempty"`

// Image defines the image to pull for the
// NFD operand
// [defaults to k8s.gcr.io/nfd/node-feature-discovery]
// +kubebuilder:validation:Pattern=[a-zA-Z0-9\-]+
Image string `json:"image,omitempty"`

// Image pull policy
// ImagePullPolicy defines Image pull policy for the
// NFD operand image [defaults to Always]
// +kubebuilder:validation:Optional
ImagePullPolicy string `json:"imagePullPolicy,omitempty"`

// ServicePort specifies the TCP port that nfd-master
// listens for incoming requests.
// +kubebuilder:validation:Optional
ServicePort int `json:"servicePort"`
}

// ConfigMap describes configuration options for the NFD worker
Expand Down
2 changes: 1 addition & 1 deletion build/assets/master/0400_master_daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spec:
fieldPath: spec.nodeName
image: $(NODE_FEATURE_DISCOVERY_IMAGE)
name: nfd-master
command: ["nfd-master", "--port=12000"]
command: ["nfd-master"]
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
Expand Down
11 changes: 10 additions & 1 deletion config/crd/bases/nfd.kubernetes.io_nodefeaturediscoveries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ spec:
description: OperandSpec describes configuration options for the operand
properties:
image:
description: Image defines the image to pull for the NFD operand
[defaults to k8s.gcr.io/nfd/node-feature-discovery]
pattern: '[a-zA-Z0-9\-]+'
type: string
imagePullPolicy:
description: Image pull policy
description: ImagePullPolicy defines Image pull policy for the
NFD operand image [defaults to Always]
type: string
namespace:
description: Namespace defines the namespace to deploy nfd-master
and nfd-worker pods
pattern: '[a-zA-Z0-9\.\-\/]+'
type: string
servicePort:
description: ServicePort specifies the TCP port that nfd-master
listens for incoming requests.
type: integer
type: object
workerConfig:
description: ConfigMap describes configuration options for the NFD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ spec:
namespace: node-feature-discovery-operator
image: gcr.io/k8s-staging-nfd/node-feature-discovery:master
imagePullPolicy: Always
servicePort: 24000
workerConfig:
configData: |
#core:
Expand Down
22 changes: 22 additions & 0 deletions controllers/nodefeaturediscovery_controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package controllers

import (
"context"
"fmt"

secv1 "github.com/openshift/api/security/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

Expand All @@ -35,6 +37,8 @@ type ResourceStatus int
const (
Ready ResourceStatus = iota
NotReady

servicePort int = 12000
)

func (s ResourceStatus) String() string {
Expand Down Expand Up @@ -305,6 +309,15 @@ func DaemonSet(n NFD) (ResourceStatus, error) {
obj.Spec.Template.Spec.Containers[0].ImagePullPolicy = n.ins.Spec.Operand.ImagePolicy(n.ins.Spec.Operand.ImagePullPolicy)
}

// update nfd-master service port
if n.ins.Spec.Operand.ServicePort != 0 && obj.ObjectMeta.Name == "nfd-master" {
portFlag := fmt.Sprintf("--port=%d", n.ins.Spec.Operand.ServicePort)
obj.Spec.Template.Spec.Containers[0].Args = []string{portFlag}
} else if obj.ObjectMeta.Name == "nfd-master" {
portFlag := fmt.Sprintf("--port=%d", servicePort)
obj.Spec.Template.Spec.Containers[0].Args = []string{portFlag}
}

obj.SetNamespace(n.ins.GetNamespace())

found := &appsv1.DaemonSet{}
Expand Down Expand Up @@ -343,6 +356,15 @@ func Service(n NFD) (ResourceStatus, error) {
state := n.idx
obj := n.resources[state].Service

// update ports
if n.ins.Spec.Operand.ServicePort != 0 {
obj.Spec.Ports[0].Port = int32(n.ins.Spec.Operand.ServicePort)
obj.Spec.Ports[0].TargetPort = intstr.FromInt(n.ins.Spec.Operand.ServicePort)
} else {
obj.Spec.Ports[0].Port = int32(servicePort)
obj.Spec.Ports[0].TargetPort = intstr.FromInt(servicePort)
}

obj.SetNamespace(n.ins.GetNamespace())

found := &corev1.Service{}
Expand Down

0 comments on commit 565899e

Please sign in to comment.