-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from razo7/downward-api
Use Downward API for Namespace
- Loading branch information
Showing
6 changed files
with
80 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// deployNamespaceEnv is a constant for env variable DEPLOYMENT_NAMESPACE | ||
// which specifies the Namespace that the operator's deployment was installed/run. | ||
// It has been set using Downward API (https://kubernetes.io/docs/concepts/workloads/pods/downward-api/) in manager.yaml | ||
const deployNamespaceEnv = "DEPLOYMENT_NAMESPACE" | ||
|
||
// GetDeploymentNamespace returns the Namespace this operator is deployed/installed on. | ||
func GetDeploymentNamespace() (string, error) { | ||
|
||
ns, found := os.LookupEnv(deployNamespaceEnv) | ||
if !found { | ||
return "", fmt.Errorf("%s must be set", deployNamespaceEnv) | ||
} | ||
return ns, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
apiErrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/selection" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// GetFenceAgentsRemediationPod fetches the FAR pod based on FAR's label and namespace | ||
func GetFenceAgentsRemediationPod(nodeName string, r client.Reader) (*corev1.Pod, error) { | ||
pods := &corev1.PodList{} | ||
|
||
selector := labels.NewSelector() | ||
requirement, _ := labels.NewRequirement("app", selection.Equals, []string{"fence-agents-remediation-operator"}) | ||
selector = selector.Add(*requirement) | ||
podNamespace, _ := GetDeploymentNamespace() | ||
|
||
err := r.List(context.Background(), pods, &client.ListOptions{LabelSelector: selector, Namespace: podNamespace}) | ||
if err != nil { | ||
fmt.Printf("failed fetching FAR pod") | ||
return nil, err | ||
} | ||
if len(pods.Items) == 0 { | ||
fmt.Printf("No Fence Agent pods were found") | ||
podNotFoundErr := &apiErrors.StatusError{ErrStatus: metav1.Status{ | ||
Status: metav1.StatusFailure, | ||
Code: http.StatusNotFound, | ||
Reason: metav1.StatusReasonNotFound, | ||
}} | ||
return nil, podNotFoundErr | ||
} | ||
|
||
return &pods.Items[0], nil | ||
} |