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

Get Cluster Information for Running FA on AWS and Test Nodes Status #32

Merged
merged 10 commits into from
May 7, 2023
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
6 changes: 3 additions & 3 deletions controllers/fenceagentsremediation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

"github.com/medik8s/fence-agents-remediation/api/v1alpha1"
"github.com/medik8s/fence-agents-remediation/pkg/cli"
"github.com/medik8s/fence-agents-remediation/pkg/utils"
farUtils "github.com/medik8s/fence-agents-remediation/pkg/utils"
)

// FenceAgentsRemediationReconciler reconciles a FenceAgentsRemediation object
Expand Down Expand Up @@ -82,13 +82,13 @@ func (r *FenceAgentsRemediationReconciler) Reconcile(ctx context.Context, req ct
// TODO: Validate FAR CR name to nodeName. Run isNodeNameValid
// Fetch the FAR's pod
r.Log.Info("Fetch FAR's pod")
pod, err := utils.GetFenceAgentsRemediationPod(req.Name, r.Client)
pod, err := farUtils.GetFenceAgentsRemediationPod(r.Client)
if err != nil {
return emptyResult, err
}
//TODO: Check that FA is excutable? run cli.IsExecuteable

r.Log.Info("Create and execute the fence agent", "Fence Agent", far.Spec.Agent)
r.Log.Info("Create and execute the fence agent", "Fence Agent", far.Spec.Agent, "Node Name", req.Name)
faParams, err := buildFenceAgentParams(far)
if err != nil {
return emptyResult, err
Expand Down
2 changes: 1 addition & 1 deletion controllers/fenceagentsremediation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var _ = Describe("FAR Controller", func() {
})
})

// newFenceAgentsRemediation assign the input to the FenceAgentsRemediation's Spec
// newFenceAgentsRemediation assigns the input to the FenceAgentsRemediation
func newFenceAgentsRemediation(nodeName string, agent string, sharedparameters map[v1alpha1.ParameterName]string, nodeparameters map[v1alpha1.ParameterName]map[v1alpha1.NodeName]string) *v1alpha1.FenceAgentsRemediation {
return &v1alpha1.FenceAgentsRemediation{
ObjectMeta: metav1.ObjectMeta{Name: nodeName, Namespace: defaultNamespace},
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ require (
github.com/go-logr/logr v1.2.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
github.com/openshift/api v0.0.0-20210831091943-07e756545ac1
github.com/openshift/client-go v0.0.0-20210831095141-e19a065e79f7
github.com/openshift/machine-api-operator v0.2.1-0.20200520080344-fe76daf636f4
go.uber.org/zap v1.19.1
k8s.io/api v0.23.5
k8s.io/apimachinery v0.23.5
Expand Down
355 changes: 355 additions & 0 deletions go.sum

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pkg/utils/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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)
Expand Down
19 changes: 11 additions & 8 deletions pkg/utils/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,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"
ctrl "sigs.k8s.io/controller-runtime"
"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) {
func GetFenceAgentsRemediationPod(r client.Reader) (*corev1.Pod, error) {
logger := ctrl.Log.WithName("utils-pods")
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})
var podNamespace string
podNamespace, err := GetDeploymentNamespace()
if err != nil {
logger.Error(err, "failed fetching FAR namespace")
}
err = r.List(context.Background(), pods, &client.ListOptions{LabelSelector: selector, Namespace: podNamespace})
if err != nil {
fmt.Printf("failed fetching FAR pod")
logger.Error(err, "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,
}}
logger.Error(podNotFoundErr, "No Fence Agent pods were found")
return nil, podNotFoundErr
}

Expand Down
36 changes: 30 additions & 6 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@ package e2e

import (
"fmt"
"os"
"testing"

"github.com/go-logr/logr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/zap/zapcore"

"github.com/medik8s/fence-agents-remediation/api/v1alpha1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
machineclient "github.com/openshift/machine-api-operator/pkg/generated/clientset/versioned/typed/machine/v1beta1"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/medik8s/fence-agents-remediation/api/v1alpha1"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
const operatorInstalledNamespcae = "OPERATOR_NS"

var (
log logr.Logger
clientSet *kubernetes.Clientset
k8sClient ctrl.Client
log logr.Logger
clientSet *kubernetes.Clientset
k8sClient ctrl.Client
configClient configclient.Interface
machineClient *machineclient.MachineV1beta1Client

// The ns the operator is running in
operatorNsName string
)

func TestE2e(t *testing.T) {
Expand All @@ -40,17 +50,31 @@ var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseFlagOptions(&opts)))
log = logf.Log

operatorNsName = os.Getenv(operatorInstalledNamespcae)
Expect(operatorNsName).ToNot(BeEmpty(), operatorInstalledNamespcae+" env var not set, can't start e2e test")

// +kubebuilder:scaffold:scheme

// get the k8sClient or die
// Load the Kubernetes configuration from the default location or from a specified kubeconfig file or simply die
config, err := config.GetConfig()
if err != nil {
Fail(fmt.Sprintf("Couldn't get kubeconfig %v", err))
}

configClient, err = configclient.NewForConfig(config)
Expect(err).NotTo(HaveOccurred())
Expect(configClient).NotTo(BeNil())

// Create a Kubernetes clientset using the configuration
clientSet, err = kubernetes.NewForConfig(config)
Expect(err).NotTo(HaveOccurred())
Expect(clientSet).NotTo(BeNil())

// Create a Machine clientset using the configuration
machineClient, err = machineclient.NewForConfig(config)
Expect(err).NotTo(HaveOccurred())
Expect(machineClient).NotTo(BeNil())

scheme.AddToScheme(scheme.Scheme)
err = v1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
Expand Down
Loading