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

PWX-35477 : Support Openshift Prometheus for portworx monitoring on OCP 4.14 #1410

Merged
merged 19 commits into from
Feb 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ getgrafanaconfigs:
cp deploy/grafana/* bin/configs/

getconfigs: cleanconfigs getccmconfigs getpluginconfigs getgrafanaconfigs getwindowsconfig
wget -q '$(PX_DOC_HOST)/samples/k8s/pxc/portworx-prometheus-rule.yaml' -P bin/configs --no-check-certificate
wget -q '$(PX_DOC_HOST)/samples/portworx-enterprise/k8s/pxc/portworx-prometheus-rule.yaml' -P bin/configs --no-check-certificate
wget -q '$(PROMETHEUS_OPERATOR_CRD_URL_PREFIX)/crd-alertmanagerconfigs.yaml' -O bin/configs/prometheus-crd-alertmanagerconfigs.yaml
wget -q '$(PROMETHEUS_OPERATOR_CRD_URL_PREFIX)/crd-alertmanagers.yaml' -O bin/configs/prometheus-crd-alertmanagers.yaml
wget -q '$(PROMETHEUS_OPERATOR_CRD_URL_PREFIX)/crd-podmonitors.yaml' -O bin/configs/prometheus-crd-podmonitors.yaml
Expand Down
5 changes: 5 additions & 0 deletions cmd/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/libopenstorage/operator/pkg/version"
ocp_configv1 "github.com/openshift/api/config/v1"
consolev1 "github.com/openshift/api/console/v1"
routev1 "github.com/openshift/api/route/v1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
Expand Down Expand Up @@ -228,6 +229,10 @@ func run(c *cli.Context) {
log.Fatalf("Failed to add cluster API resources to the scheme: %v", err)
}

if err := routev1.AddToScheme(mgr.GetScheme()); err != nil {
log.Fatalf("Failed to add cluster API resources to the scheme: %v", err)
}

// Create Service and ServiceMonitor objects to expose the metrics to Prometheus
metricsPort := c.Int(flagMetricsPort)
metricsServicePorts := []v1.ServicePort{
Expand Down
148 changes: 144 additions & 4 deletions drivers/storage/portworx/component/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"sort"
"strings"

"github.com/sirupsen/logrus"

"github.com/hashicorp/go-version"
pxutil "github.com/libopenstorage/operator/drivers/storage/portworx/util"
corev1 "github.com/libopenstorage/operator/pkg/apis/core/v1"
Expand Down Expand Up @@ -45,7 +47,11 @@
AutopilotDefaultProviderEndpoint = "http://px-prometheus:9090"
// AutopilotDefaultReviewersKey is a key for default reviewers array in gitops config map
AutopilotDefaultReviewersKey = "defaultReviewers"
defaultAutopilotCPU = "0.1"
// OCPPrometheusUserWorkloadSecretPrefix name of OCP user-workload Prometheus secret
OCPPrometheusUserWorkloadSecretPrefix = "prometheus-user-workload-token"
// Autopilot Secret name for prometheus-user-workload-token
AutopilotSecretName = "autopilot-prometheus-auth"
defaultAutopilotCPU = "0.1"
)

var (
Expand Down Expand Up @@ -80,12 +86,49 @@
},
},
}

openshiftDeploymentVolume = []corev1.VolumeSpec{
{
Name: "token-volume",
MountPath: "/var/local/secrets",
ReadOnly: true,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: AutopilotSecretName,
Items: []v1.KeyToPath{
{
Key: "token",
Path: "token",
},
},
},
},
},
{
Name: "ca-cert-volume",
MountPath: "/etc/ssl/certs",
ReadOnly: true,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: AutopilotSecretName,
Items: []v1.KeyToPath{
{
Key: "cacert",
Path: "ca-certificates.crt",
},
},
},
},
},
}
)

type autopilot struct {
isCreated bool
k8sClient client.Client
k8sVersion version.Version
isCreated bool
k8sClient client.Client
k8sVersion version.Version
isUserWorkloadSupported *bool
isVolumeMounted bool
}

func (c *autopilot) Name() string {
Expand Down Expand Up @@ -128,6 +171,11 @@
if err := c.createClusterRoleBinding(cluster.Namespace); err != nil {
return err
}
if c.isOCPUserWorkloadSupported() {
if err := c.createSecret(cluster.Namespace, ownerRef); err != nil {
piyush-nimbalkar marked this conversation as resolved.
Show resolved Hide resolved
return err

Check warning on line 176 in drivers/storage/portworx/component/autopilot.go

View check run for this annotation

Codecov / codecov/patch

drivers/storage/portworx/component/autopilot.go#L176

Added line #L176 was not covered by tests
}
}
if err := c.createDeployment(cluster, ownerRef); err != nil {
return err
}
Expand All @@ -151,12 +199,20 @@
if err := k8sutil.DeleteDeployment(c.k8sClient, AutopilotDeploymentName, cluster.Namespace, *ownerRef); err != nil {
return err
}
if c.isOCPUserWorkloadSupported() {
if err := k8sutil.DeleteSecret(c.k8sClient, AutopilotSecretName, cluster.Namespace, *ownerRef); err != nil {
return err

Check warning on line 204 in drivers/storage/portworx/component/autopilot.go

View check run for this annotation

Codecov / codecov/patch

drivers/storage/portworx/component/autopilot.go#L204

Added line #L204 was not covered by tests
}
}

c.MarkDeleted()
return nil
}

func (c *autopilot) MarkDeleted() {
c.isCreated = false
c.isUserWorkloadSupported = nil
c.isVolumeMounted = false
}

func (c *autopilot) createConfigMap(
Expand Down Expand Up @@ -248,6 +304,30 @@
return err
}

func (c *autopilot) createSecret(clusterNamespace string, ownerRef *metav1.OwnerReference) error {

token, cert, err := c.getPrometheusTokenAndCert()
if err != nil {
return err

Check warning on line 311 in drivers/storage/portworx/component/autopilot.go

View check run for this annotation

Codecov / codecov/patch

drivers/storage/portworx/component/autopilot.go#L311

Added line #L311 was not covered by tests
}

return k8sutil.CreateOrUpdateSecret(
c.k8sClient,
&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: AutopilotSecretName,
Namespace: clusterNamespace,
OwnerReferences: []metav1.OwnerReference{*ownerRef},
},
Data: map[string][]byte{
"token": []byte(token),
"cacert": []byte(cert),
},
},
ownerRef,
)
}

func (c *autopilot) createServiceAccount(
clusterNamespace string,
ownerRef *metav1.OwnerReference,
Expand Down Expand Up @@ -643,6 +723,12 @@
cluster *corev1.StorageCluster,
) ([]v1.Volume, []v1.VolumeMount) {
volumeSpecs := make([]corev1.VolumeSpec, 0)

if c.isOCPUserWorkloadSupported() && !c.isVolumeMounted {
c.isVolumeMounted = true
piyush-nimbalkar marked this conversation as resolved.
Show resolved Hide resolved
autopilotDeploymentVolumes = append(autopilotDeploymentVolumes, openshiftDeploymentVolume...)
}

for _, v := range autopilotDeploymentVolumes {
vCopy := v.DeepCopy()
volumeSpecs = append(volumeSpecs, *vCopy)
Expand All @@ -659,6 +745,60 @@
return volumes, volumeMounts
}

func (c *autopilot) getPrometheusTokenAndCert() (encodedToken, caCert string, err error) {
secrets := &v1.SecretList{}
err = c.k8sClient.List(
context.TODO(),
secrets,
client.InNamespace("openshift-user-workload-monitoring"),
)

if err != nil {
return "", "", err

Check warning on line 757 in drivers/storage/portworx/component/autopilot.go

View check run for this annotation

Codecov / codecov/patch

drivers/storage/portworx/component/autopilot.go#L757

Added line #L757 was not covered by tests
}

// Iterate through the secrets list to process prometheus-user-workload-token secret
var secretFound bool
for _, secret := range secrets.Items {

if strings.HasPrefix(secret.Name, OCPPrometheusUserWorkloadSecretPrefix) {
secretFound = true
// Retrieve the token data from the secret as []byte
tokenBytes, ok := secret.Data["token"]
if !ok {
return encodedToken, caCert, fmt.Errorf("token not found in secret")

Check warning on line 769 in drivers/storage/portworx/component/autopilot.go

View check run for this annotation

Codecov / codecov/patch

drivers/storage/portworx/component/autopilot.go#L769

Added line #L769 was not covered by tests
}

// Retrieve the ca.cert data from the secret as []byte
cert, ok := secret.Data["ca.crt"]
if !ok {
return encodedToken, caCert, fmt.Errorf("cert not found in secret")

Check warning on line 775 in drivers/storage/portworx/component/autopilot.go

View check run for this annotation

Codecov / codecov/patch

drivers/storage/portworx/component/autopilot.go#L775

Added line #L775 was not covered by tests
}

encodedToken = string(tokenBytes)
caCert = string(cert)
piyush-nimbalkar marked this conversation as resolved.
Show resolved Hide resolved
break
}
}

if !secretFound {
return "", "", fmt.Errorf("prometheus-user-workload-token not found. Please make sure that user workload monitoring is enabled in openshift")

Check warning on line 785 in drivers/storage/portworx/component/autopilot.go

View check run for this annotation

Codecov / codecov/patch

drivers/storage/portworx/component/autopilot.go#L785

Added line #L785 was not covered by tests
}
return encodedToken, caCert, nil
}

func (c *autopilot) isOCPUserWorkloadSupported() bool {
if c.isUserWorkloadSupported == nil {
isSupported, err := pxutil.IsSupportedOCPVersion(c.k8sClient, pxutil.OpenshiftPrometheusSupportedVersion)
if err != nil {
logrus.Errorf("Failed to check if OCP user workload monitoring is supported: %v", err)
return false

Check warning on line 795 in drivers/storage/portworx/component/autopilot.go

View check run for this annotation

Codecov / codecov/patch

drivers/storage/portworx/component/autopilot.go#L794-L795

Added lines #L794 - L795 were not covered by tests
}
c.isUserWorkloadSupported = &isSupported
}
return *c.isUserWorkloadSupported
}

// RegisterAutopilotComponent registers the Autopilot component
func RegisterAutopilotComponent() {
Register(AutopilotComponentName, &autopilot{})
Expand Down
8 changes: 4 additions & 4 deletions drivers/storage/portworx/component/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (p *plugin) IsEnabled(cluster *corev1.StorageCluster) bool {
}

for _, v := range operator.Status.Versions {
if v.Name == OpenshiftAPIServer && isVersionSupported(v.Version) {
if v.Name == OpenshiftAPIServer && isVersionSupported(v.Version, OpenshiftSupportedVersion) {
p.isPluginSupported = boolPtr(true)
return true
}
Expand Down Expand Up @@ -349,14 +349,14 @@ func updateDataIfNginxConfigMap(cm *v1.ConfigMap, storageNs string) {
}
}

func isVersionSupported(v string) bool {
targetVersion, err := version.NewVersion(OpenshiftSupportedVersion)
func isVersionSupported(current, target string) bool {
targetVersion, err := version.NewVersion(target)
if err != nil {
logrus.Errorf("Error during parsing version : %s ", err)
return false
}

currentVersion, err := version.NewVersion(v)
currentVersion, err := version.NewVersion(current)
if err != nil {
logrus.Errorf("Error during parsing version : %s ", err)
return false
Expand Down
Loading
Loading