Skip to content

Commit

Permalink
PMK-3900: Adding pf9-profile-agent addon (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
supreeth90 authored Jul 30, 2021
1 parent 87f5cfb commit 585a6eb
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
4 changes: 4 additions & 0 deletions addon_templates/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@
{
"version": "1.13.8",
"type": "cluster-auto-scaler-azure"
},
{
"version": "1.0.0",
"type": "pf9-profile-agent"
}
]
69 changes: 69 additions & 0 deletions addon_templates/pf9-profile-agent/1.0.0/pf9-profile-agent.yaml
Original file line number Diff line number Diff line change
@@ -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: ""
2 changes: 2 additions & 0 deletions pkg/addons/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
142 changes: 142 additions & 0 deletions pkg/addons/pf9-profile-agent.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions tooling/samples/agent_v1_pf9_profile_agent.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 585a6eb

Please sign in to comment.