Add ownerRef to kuberhealthy operator to checks
Don't try to point to the CRD:s, instead point the owenerRefernece to
the operator. Don't take in to account that the operator pod can be
removed.
@@ -15,7 +15,6 @@ import (
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
- "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
@@ -71,8 +70,6 @@ var kubeConfigFile = filepath.Join(os.Getenv("HOME"), ".kube", "config")
// constants for using the kuberhealthy check CRD
const CRDGroup = "comcast.github.io"
const CRDVersion = "v1"
-const CRDGroupPlusVersion = CRDGroup + "/" + CRDVersion
-const CRDKind = "KuberhealthyCheck"
const checkCRDResource = "khchecks"
const stateCRDResource = "khstates"
@@ -99,7 +96,6 @@ type Checker struct {
wg sync.WaitGroup // used to track background workers and processes
hostname string // hostname cache
checkPodName string // the current unique checker pod name
- crdUID types.UID
}
// New creates a new external checker
@@ -123,7 +119,6 @@ func New(client *kubernetes.Clientset, checkConfig *khcheckcrd.KuberhealthyCheck
OriginalPodSpec: checkConfig.Spec.PodSpec,
PodSpec: checkConfig.Spec.PodSpec,
KubeClient: client,
- crdUID: checkConfig.GetObjectMeta().GetUID(),
}
}
@@ -1031,18 +1026,40 @@ func (ext *Checker) createPod() (*apiv1.Pod, error) {
// enforce various labels and annotations on all checker pods created
ext.addKuberhealthyLabels(p)
+ // Get the hostname of the kuberhealthy pod
+ kPodName, err := os.Hostname()
+ if err != nil {
+ ext.log("Unable to get kuberhealthy pod podname")
+ }
+
+ // Get the podspec of kuberhealthy pod
+ kHealthyPod, err := ext.getKuberHealthyPod(kPodName)
+ if err != nil {
+ ext.log("Unable to find kuberhealthy pod", err)
+ } else {
// Sets OwnerRefernces on all checker pods
p.OwnerReferences = []metav1.OwnerReference{
{
- APIVersion: CRDGroupPlusVersion,
- Kind: CRDKind,
- Name: ext.CheckName,
- UID: ext.crdUID,
+ APIVersion: "v1",
+ Kind: "Pod",
+ Name: kPodName,
+ UID: kHealthyPod.GetUID(),
},
}
+ }
return ext.KubeClient.CoreV1().Pods(ext.Namespace).Create(p)
}
+// getKuberHealthyPod gets the pod spec of kuberhealthy operator
+func (ext *Checker) getKuberHealthyPod(kPodName string) (*apiv1.Pod, error) {
+ podClient := ext.KubeClient.CoreV1().Pods(ext.Namespace)
+ kHealthyPod, err := podClient.Get(kPodName, metav1.GetOptions{})
+ if err != nil {
+ return nil, err
+ }
+ return kHealthyPod, nil
+}
+
// configureUserPodSpec configures a user-specified pod spec with
// the unique and required fields for compatibility with an external
// kuberhealthy check. Required environment variables and settings
