diff --git a/addon_templates/manifest.json b/addon_templates/manifest.json index db1679bd..5ca2dbc7 100644 --- a/addon_templates/manifest.json +++ b/addon_templates/manifest.json @@ -26,5 +26,9 @@ { "version": "1.13.8", "type": "cluster-auto-scaler-azure" + }, + { + "version": "1.0.0", + "type": "pf9-profile-agent" } ] diff --git a/addon_templates/pf9-profile-agent/1.0.0/pf9-profile-agent.yaml b/addon_templates/pf9-profile-agent/1.0.0/pf9-profile-agent.yaml new file mode 100644 index 00000000..ff5f2a60 --- /dev/null +++ b/addon_templates/pf9-profile-agent/1.0.0/pf9-profile-agent.yaml @@ -0,0 +1,69 @@ +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: pf9-profile-agent + namespace: platform9-system +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: pf9-profile-agent +rules: + - apiGroups: ["*"] + resources: ["*"] + verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: pf9-profile-agent +subjects: + - kind: ServiceAccount + name: pf9-profile-agent + namespace: platform9-system +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: pf9-profile-agent +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: pf9-addons + name: pf9-profile-agent + namespace: platform9-system +spec: + selector: + matchLabels: + app: pf9-addons + template: + metadata: + labels: + app: pf9-addons + spec: + serviceAccountName: pf9-profile-agent + hostNetwork: true + containers: + - name: pf9-profile-agent + image: {{ .DockerRegistry }}/platform9/pf9-profile-agent:1.0.0 + imagePullPolicy: IfNotPresent + resources: + limits: + memory: "128Mi" + cpu: "500m" + env: + - name: CLUSTER_ID + value: {{ .ClusterId }} + - name: PROJECT_ID + value: {{ .ProjectId }} + - name: DEBUG + value: "true" + tolerations: + - effect: NoSchedule + key: node-role.kubernetes.io/master + operator: Equal + value: "true" + nodeSelector: + node-role.kubernetes.io/master: "" diff --git a/pkg/addons/client.go b/pkg/addons/client.go index 5f90b5c4..f33293c5 100644 --- a/pkg/addons/client.go +++ b/pkg/addons/client.go @@ -35,6 +35,8 @@ func getAddonClient(mode, version string, params map[string]interface{}, c clien instance = newKubeVirt(c, version, params) case "luigi": instance = newLuigi(c, version, params) + case "pf9-profile-agent": + instance = newProfileAgent(c, version, params) default: log.Errorf("Mode %s is not supported", mode) diff --git a/pkg/addons/pf9-profile-agent.go b/pkg/addons/pf9-profile-agent.go new file mode 100644 index 00000000..612f0820 --- /dev/null +++ b/pkg/addons/pf9-profile-agent.go @@ -0,0 +1,142 @@ +package addons + +import ( + "os" + "path/filepath" + + "github.com/platform9/pf9-addon-operator/pkg/util" + log "github.com/sirupsen/logrus" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +const ( + profileAgentNs = "platform9-system" + profileAgentDeployment = "pf9-profile-agent" + profileAgentDir = "pf9-profile-agent" +) + +// ProfileAgentClient represents the pf9-profile-agent addon +type ProfileAgentClient struct { + client client.Client + overrideParams map[string]interface{} + version string +} + +func newProfileAgent(c client.Client, version string, params map[string]interface{}) *ProfileAgentClient { + + cl := &ProfileAgentClient{ + client: c, + overrideParams: params, + version: version, + } + + return cl +} + +//overrideRegistry checks if we need to override container registry values +func (c *ProfileAgentClient) overrideRegistry() { + overrideRegistry := util.GetRegistry(envVarDockerRegistry, "docker.io") + c.overrideParams[templateDockerRegistry] = overrideRegistry + + log.Infof("Using container registry: %s", c.overrideParams[templateDockerRegistry]) +} + +//ValidateParams validates params of an addon +func (c *ProfileAgentClient) ValidateParams() (bool, error) { + + return true, nil +} + +//Health checks health of the pf9-profile-agent instance +func (c *ProfileAgentClient) Health() (bool, error) { + deploy, err := util.GetDeployment(profileAgentNs, profileAgentDeployment, c.client) + if err != nil { + log.Errorf("Failed to get deployment: %s", err) + return false, err + } + + if deploy == nil { + return false, nil + } + + if deploy.Status.ReadyReplicas > 0 { + return true, nil + } + + return false, nil +} + +//Upgrade upgrades an pf9-profile-agent instance +func (c *ProfileAgentClient) Upgrade() error { + return c.Install() +} + +//Install installs an pf9-profile-agent instance +func (c *ProfileAgentClient) Install() error { + + inputPath, outputPath, err := util.EnsureDirStructure(profileAgentDir, c.version) + if err != nil { + return err + } + + inputFilePath := filepath.Join(inputPath, "pf9-profile-agent.yaml") + outputFilePath := filepath.Join(outputPath, "pf9-profile-agent.yaml") + + c.overrideRegistry() + + // Fetch and add clusterID/projectID to the overrideParams + clusterID := os.Getenv(util.ClusterIDEnvVar) + projectID := os.Getenv(util.ProjectIDEnvVar) + c.overrideParams["ClusterId"] = clusterID + c.overrideParams["ProjectId"] = projectID + + labels := []util.Labels{} + + err = util.CreateNsIfNeeded(profileAgentNs, labels, c.client) + if err != nil { + log.Errorf("Failed to create ns: %s %s", profileAgentNs, err) + return err + } + + err = util.WriteConfigToTemplate(inputFilePath, outputFilePath, "pf9-profile-agent.yaml", c.overrideParams) + if err != nil { + log.Errorf("Failed to write output file: %s", err) + return err + } + + err = util.ApplyYaml(outputFilePath, c.client) + if err != nil { + log.Errorf("Failed to apply yaml file: %s", err) + return err + } + + return nil +} + +//Uninstall removes an dashboard instance +func (c *ProfileAgentClient) Uninstall() error { + + inputPath, outputPath, err := util.EnsureDirStructure(profileAgentDir, c.version) + if err != nil { + return err + } + + inputFilePath := filepath.Join(inputPath, "pf9-profile-agent.yaml") + outputFilePath := filepath.Join(outputPath, "pf9-profile-agent.yaml") + + c.overrideRegistry() + + err = util.WriteConfigToTemplate(inputFilePath, outputFilePath, "pf9-profile-agent.yaml", c.overrideParams) + if err != nil { + log.Errorf("Failed to write output file: %s", err) + return err + } + + err = util.DeleteYaml(outputFilePath, c.client) + if err != nil { + log.Errorf("Failed to delete yaml file: %s", err) + return err + } + + return nil +} diff --git a/tooling/samples/agent_v1_pf9_profile_agent.yaml b/tooling/samples/agent_v1_pf9_profile_agent.yaml new file mode 100644 index 00000000..96359c8b --- /dev/null +++ b/tooling/samples/agent_v1_pf9_profile_agent.yaml @@ -0,0 +1,8 @@ +apiVersion: agent.pf9.io/v1 +kind: Addon +metadata: + name: pf9-profile-agent +spec: + version: 1.0.0 + type: "pf9-profile-agent" + watch: true