Skip to content

Commit

Permalink
Add plugin version and image tags as annotations to the resources (#886)
Browse files Browse the repository at this point in the history
```
directpv.min.io/image-tag
directpv.min.io/plugin-version
```

are the annotations added to the components
  • Loading branch information
Praveenrajmani authored Dec 15, 2023
1 parent 941907b commit c580047
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 48 deletions.
6 changes: 6 additions & 0 deletions cmd/kubectl-directpv/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ func installMain(ctx context.Context) {
os.Exit(1)
}

pluginVersion := "dev"
if Version != "" {
pluginVersion = Version
}

args.Registry = registry
args.Org = org
args.ImagePullSecrets = imagePullSecrets
Expand All @@ -290,6 +295,7 @@ func installMain(ctx context.Context) {
args.Quiet = quietFlag
args.KubeVersion = kubeVersion
args.Legacy = legacyFlag
args.PluginVersion = pluginVersion
if file != nil {
args.ObjectWriter = file
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/directpv.min.io/types/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ const (

// ClaimIDLabelKey label key to denote the claim id of the volumes
ClaimIDLabelKey LabelKey = consts.GroupName + "/claim-id"

// ImageTagLabelKey denotes the tag of the directpv container image
ImageTagLabelKey LabelKey = consts.GroupName + "/image-tag"

// PluginVersionLabelKey denotes the plugin version
PluginVersionLabelKey LabelKey = consts.GroupName + "/plugin-version"
)

// LabelValue is a type definition for label value
Expand Down
11 changes: 11 additions & 0 deletions pkg/installer/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"path"
"regexp"

"github.com/minio/directpv/pkg/utils"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -71,16 +72,25 @@ type Args struct {
ObjectMarshaler func(runtime.Object) ([]byte, error)
ProgressCh chan<- Message
ForceUninstall bool
PluginVersion string

podSecurityAdmission bool
csiProvisionerImage string
nodeDriverRegistrarImage string
livenessProbeImage string
csiResizerImage string
imageTag string
}

var imageTagRegex = regexp.MustCompile(`:([^/]+)$`)

// NewArgs creates arguments for DirectPV installation.
func NewArgs(image string) *Args {
imageTag := "dev"
matchIndex := imageTagRegex.FindStringSubmatchIndex(image)
if len(matchIndex) > 0 && len(image) > matchIndex[0]+1 {
imageTag = image[matchIndex[0]+1:]
}
return &Args{
image: image,
Registry: "quay.io",
Expand All @@ -90,6 +100,7 @@ func NewArgs(image string) *Args {
nodeDriverRegistrarImage: nodeDriverRegistrarImage,
livenessProbeImage: livenessProbeImage,
csiResizerImage: csiResizerImage,
imageTag: imageTag,
}
}

Expand Down
21 changes: 13 additions & 8 deletions pkg/installer/csidriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/k8s"
legacyclient "github.com/minio/directpv/pkg/legacy/client"
Expand Down Expand Up @@ -90,10 +91,12 @@ func doCreateCSIDriver(ctx context.Context, args *Args, version string, legacy b
Kind: "CSIDriver",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{},
Labels: defaultLabels,
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Spec: storagev1.CSIDriverSpec{
PodInfoOnMount: &podInfoOnMount,
Expand Down Expand Up @@ -121,10 +124,12 @@ func doCreateCSIDriver(ctx context.Context, args *Args, version string, legacy b
Kind: "CSIDriver",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{},
Labels: defaultLabels,
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Spec: storagev1beta1.CSIDriverSpec{
PodInfoOnMount: &podInfoOnMount,
Expand Down
22 changes: 13 additions & 9 deletions pkg/installer/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/k8s"
legacyclient "github.com/minio/directpv/pkg/legacy/client"
Expand Down Expand Up @@ -203,12 +204,12 @@ func livenessProbeContainer(image string) corev1.Container {
}
}

func newDaemonset(podSpec corev1.PodSpec, name, selectorValue, appArmorProfile string) *appsv1.DaemonSet {
func newDaemonset(podSpec corev1.PodSpec, name, selectorValue string, args *Args) *appsv1.DaemonSet {
annotations := map[string]string{createdByLabel: pluginName}
if appArmorProfile != "" {
if args.AppArmorProfile != "" {
// AppArmor profiles need to be specified per-container
for _, container := range podSpec.Containers {
annotations["container.apparmor.security.beta.kubernetes.io/"+container.Name] = "localhost/" + appArmorProfile
annotations["container.apparmor.security.beta.kubernetes.io/"+container.Name] = "localhost/" + args.AppArmorProfile
}
}

Expand All @@ -218,10 +219,13 @@ func newDaemonset(podSpec corev1.PodSpec, name, selectorValue, appArmorProfile s
Kind: "DaemonSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{},
Labels: defaultLabels,
Name: name,
Namespace: namespace,
Annotations: map[string]string{
string(directpvtypes.ImageTagLabelKey): args.imageTag,
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Spec: appsv1.DaemonSetSpec{
Selector: metav1.AddLabelToSelector(&metav1.LabelSelector{}, selectorKey, selectorValue),
Expand Down Expand Up @@ -302,7 +306,7 @@ func doCreateDaemonset(ctx context.Context, args *Args) (err error) {
selectorValue = fmt.Sprintf("%v-%v", consts.Identity, getRandSuffix())
}

daemonset := newDaemonset(podSpec, consts.NodeServerName, selectorValue, args.AppArmorProfile)
daemonset := newDaemonset(podSpec, consts.NodeServerName, selectorValue, args)

if !args.DryRun && !args.Declarative {
_, err = k8s.KubeClient().AppsV1().DaemonSets(namespace).Create(
Expand Down Expand Up @@ -365,7 +369,7 @@ func doCreateLegacyDaemonset(ctx context.Context, args *Args) (err error) {
selectorValue = fmt.Sprintf("%v-%v", consts.Identity, getRandSuffix())
}

daemonset := newDaemonset(podSpec, consts.LegacyNodeServerName, selectorValue, args.AppArmorProfile)
daemonset := newDaemonset(podSpec, consts.LegacyNodeServerName, selectorValue, args)

if !args.DryRun && !args.Declarative {
_, err = k8s.KubeClient().AppsV1().DaemonSets(namespace).Create(
Expand Down
12 changes: 8 additions & 4 deletions pkg/installer/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/k8s"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -201,10 +202,13 @@ func doCreateDeployment(ctx context.Context, args *Args, legacy bool, step int)
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{},
Labels: defaultLabels,
Name: name,
Namespace: namespace,
Annotations: map[string]string{
string(directpvtypes.ImageTagLabelKey): args.imageTag,
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Expand Down
13 changes: 8 additions & 5 deletions pkg/installer/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package installer
import (
"context"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/k8s"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -87,11 +88,13 @@ func createNamespace(ctx context.Context, args *Args) (err error) {
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{},
Labels: labels,
Finalizers: []string{metav1.FinalizerDeleteDependents},
Name: namespace,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: labels,
Finalizers: []string{metav1.FinalizerDeleteDependents},
},
}

Expand Down
21 changes: 13 additions & 8 deletions pkg/installer/psp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/k8s"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -87,10 +88,12 @@ func createPSPClusterRoleBinding(ctx context.Context, args *Args) (err error) {
Kind: "ClusterRoleBinding",
},
ObjectMeta: metav1.ObjectMeta{
Name: pspClusterRoleBindingName,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{},
Labels: defaultLabels,
Name: pspClusterRoleBindingName,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Subjects: []rbac.Subject{
{
Expand Down Expand Up @@ -133,10 +136,12 @@ func createPodSecurityPolicy(ctx context.Context, args *Args) (err error) {
Kind: "PodSecurityPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Name: consts.Identity,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{},
Labels: defaultLabels,
Name: consts.Identity,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Spec: policy.PodSecurityPolicySpec{
Privileged: true,
Expand Down
15 changes: 11 additions & 4 deletions pkg/installer/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package installer
import (
"context"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/k8s"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -94,10 +95,12 @@ func createServiceAccount(ctx context.Context, args *Args) (err error) {
Kind: "ServiceAccount",
},
ObjectMeta: metav1.ObjectMeta{
Name: consts.Identity,
Namespace: namespace,
Annotations: map[string]string{},
Labels: defaultLabels,
Name: consts.Identity,
Namespace: namespace,
Annotations: map[string]string{
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Secrets: []corev1.ObjectReference{},
ImagePullSecrets: []corev1.LocalObjectReference{},
Expand Down Expand Up @@ -137,6 +140,7 @@ func createClusterRole(ctx context.Context, args *Args) (err error) {
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
"rbac.authorization.kubernetes.io/autoupdate": "true",
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Expand Down Expand Up @@ -203,6 +207,7 @@ func createClusterRoleBinding(ctx context.Context, args *Args) (err error) {
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
"rbac.authorization.kubernetes.io/autoupdate": "true",
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Expand Down Expand Up @@ -253,6 +258,7 @@ func createRole(ctx context.Context, args *Args) (err error) {
Namespace: namespace,
Annotations: map[string]string{
"rbac.authorization.kubernetes.io/autoupdate": "true",
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Expand Down Expand Up @@ -294,6 +300,7 @@ func createRoleBinding(ctx context.Context, args *Args) (err error) {
Namespace: namespace,
Annotations: map[string]string{
"rbac.authorization.kubernetes.io/autoupdate": "true",
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
},
Expand Down
24 changes: 14 additions & 10 deletions pkg/installer/storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ func doCreateStorageClass(ctx context.Context, args *Args, version string, legac
storageClass := &storagev1.StorageClass{
TypeMeta: metav1.TypeMeta{APIVersion: "storage.k8s.io/v1", Kind: "StorageClass"},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{},
Labels: defaultLabels,
Finalizers: []string{metav1.FinalizerDeleteDependents},
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
Finalizers: []string{metav1.FinalizerDeleteDependents},
},
Provisioner: consts.Identity,
AllowVolumeExpansion: &allowExpansion,
Expand Down Expand Up @@ -131,11 +133,13 @@ func doCreateStorageClass(ctx context.Context, args *Args, version string, legac
storageClass := &storagev1beta1.StorageClass{
TypeMeta: metav1.TypeMeta{APIVersion: "storage.k8s.io/v1beta1", Kind: "StorageClass"},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{},
Labels: defaultLabels,
Finalizers: []string{metav1.FinalizerDeleteDependents},
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
string(directpvtypes.PluginVersionLabelKey): args.PluginVersion,
},
Labels: defaultLabels,
Finalizers: []string{metav1.FinalizerDeleteDependents},
},
Provisioner: consts.Identity,
AllowVolumeExpansion: &allowExpansion,
Expand Down

0 comments on commit c580047

Please sign in to comment.