Skip to content

Commit

Permalink
[monitoring] Remove leftover secret from temporary Grafana 10 domain …
Browse files Browse the repository at this point in the history
…certificate (#9703)

* [prometheus] hook for deleting old grafanav8 certs

Signed-off-by: alexey.komyakov <[email protected]>

---------

Signed-off-by: alexey.komyakov <[email protected]>
  • Loading branch information
scaps1 authored Sep 20, 2024
1 parent 7507b65 commit 2b89007
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,4 @@ replace go.cypherpunks.ru/gogost/v5 v5.13.0 => github.com/flant/gogost/v5 v5.13.
// swag v0.22+ breaks schemas_test.go:TestMapMergeAnchor, seems it doesn't support anchoring. Have to figure out that.
replace github.com/go-openapi/swag => github.com/go-openapi/swag v0.21.1

replace github.com/deckhouse/deckhouse/go_lib/registry-packages-proxy => ./go_lib/registry-packages-proxy
replace github.com/deckhouse/deckhouse/go_lib/registry-packages-proxy => ./go_lib/registry-packages-proxy
78 changes: 78 additions & 0 deletions modules/300-prometheus/hooks/delete_certificate_secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2024 Flant JSC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hooks

import (
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
"github.com/flant/shell-operator/pkg/kube_events_manager/types"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type Secret struct {
apiVersion string
kind string
namespace string
name string
}

var _ = sdk.RegisterFunc(&go_hook.HookConfig{
OnBeforeHelm: &go_hook.OrderedConfig{Order: 10},
Kubernetes: []go_hook.KubernetesConfig{
{
Name: "secret",
ApiVersion: "v1",
Kind: "Secret",
NameSelector: &types.NameSelector{MatchNames: []string{"ingress-tls-v10"}},
FilterFunc: applySecretFilter,
NamespaceSelector: &types.NamespaceSelector{
NameSelector: &types.NameSelector{
MatchNames: []string{"d8-monitoring"},
},
},
},
},
}, removeSecretGrfana)

func applySecretFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
var secret corev1.Secret
err := sdk.FromUnstructured(obj, &secret)
if err != nil {
return "", err
}

return &Secret{
name: secret.Name,
namespace: secret.Namespace,
kind: secret.Kind,
apiVersion: secret.APIVersion,
}, nil
}

func removeSecretGrfana(input *go_hook.HookInput) error {
if secretSnapshot := input.Snapshots["secret"]; len(secretSnapshot) > 0 {
for _, snap := range secretSnapshot {
secret := snap.(*Secret)
log.Debugf("Deleting secret: %s/%s", secret.namespace, secret.name)
input.PatchCollector.Delete(secret.apiVersion, secret.kind, secret.namespace, secret.name)
}
} else {
log.Debug("Secrets not found")
}

return nil
}
2 changes: 1 addition & 1 deletion modules/300-prometheus/templates/grafana/certificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metadata:
namespace: d8-monitoring
{{- include "helm_lib_module_labels" (list . (dict "app" "grafana")) | nindent 2 }}
spec:
certificateOwnerRef: false
certificateOwnerRef: true
secretName: {{ include "helm_lib_module_https_secret_name" (list . "ingress-tls") }}
{{ include "helm_lib_module_generate_common_name" (list . "grafana") | nindent 2 }}
dnsNames:
Expand Down

0 comments on commit 2b89007

Please sign in to comment.