This commit is contained in:
Preeti Bansal 2020-01-07 16:27:01 +05:30
Родитель bc4277c7e8
Коммит a658a57c84
7 изменённых файлов: 108 добавлений и 118 удалений

Просмотреть файл

@ -4,7 +4,7 @@ metadata:
name: azurepipelinespool-operator
namespace: azuredevops
spec:
controllerImage: prebansa/k8s-poolprovider:v2.16
controllerImage: prebansa/k8s-poolprovider:v2.17
buildkitReplicas: 1
agentPools:
- name: windows
@ -12,20 +12,8 @@ spec:
containers:
- name: vsts-agent
image: prebansa/myagent:v5.17
env:
- name: RUNNING_ON
valueFrom:
configMapKeyRef:
name: kubernetes-config
key: type
- name: linux
spec:
containers:
- name: vsts-agent
image: prebansa/myagent:v6.2
env:
- name: RUNNING_ON
valueFrom:
configMapKeyRef:
name: kubernetes-config
key: type
image: prebansa/myagent:v6.2

Просмотреть файл

@ -128,8 +128,7 @@ func TestReleaseHandlerShouldBeSuccessful(t *testing.T) {
}
func TestGetBuildPodHandlerShouldBeSuccessful(t *testing.T) {
SetTestingEnvironmentVariables()
CreateDummyBuildKitPod()
SetTestingEnvironmentVariables(true)
var response PodResponse
var jsonStr = []byte("")

Просмотреть файл

@ -4,6 +4,9 @@ import (
"os"
"path/filepath"
v1alpha1 "github.com/microsoft/k8s-poolprovider/pkg/apis/dev/v1alpha1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
@ -66,9 +69,7 @@ func homeDir() string {
func CreateClientSet() *k8s {
testingMode := os.Getenv("COUNTTEST")
if testingMode == "1" {
if v1alpha1.IsTestingEnv() {
if client.clientset == nil {
client.clientset = fake.NewSimpleClientset()
}
@ -79,17 +80,44 @@ func CreateClientSet() *k8s {
return &client
}
func isTestingEnv() bool {
testingMode := os.Getenv("COUNTTEST")
if testingMode == "1" {
return true
}
return false
}
func SetTestingEnvironmentVariables() {
os.Setenv("COUNTTEST", "1")
func SetTestingEnvironmentVariables(params ...bool) {
os.Setenv("IS_TESTENVIRONMENT", "true")
os.Setenv("VSTS_SECRET", "sharedsecret1234")
client.clientset = fake.NewSimpleClientset()
if len(params) == 0 {
params = append(params, false)
}
CreateDummyPod(params[0])
}
func CreateDummyPod(isbuildkit bool) {
cs := CreateClientSet()
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "azurepipelinesagentpod",
Namespace: "azuredevops",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "agentimage",
Image: "prebansa/myagent:v5.16",
},
},
},
}
pod.SetLabels(map[string]string{
"app": "azurepipelinespool-operator",
})
if isbuildkit {
pod.SetLabels(map[string]string{
"role": "buildkit",
})
pod.ObjectMeta.Name = "buildkitd-0"
}
podClient := cs.clientset.CoreV1().Pods("azuredevops")
_, _ = podClient.Create(pod)
}

Просмотреть файл

@ -38,31 +38,21 @@ func CreatePod(agentRequest AgentRequest, podnamespace string) AgentProvisionRes
var pod *v1.Pod
var crdclient *v1alpha1.AzurePipelinesPoolV1Alpha1Client
if isTestingEnv() {
crdclient, _ = v1alpha1.NewClientTest()
} else {
crdclient, _ = v1alpha1.NewClient(config)
log.Println("rest client inside getspecification \n", crdclient)
}
crdclient, _ = v1alpha1.NewClient(config)
crdobject, err := crdclient.AzurePipelinesPool(podnamespace).Get("azurepipelinespool-operator")
if err != nil {
log.Println("error fetching crdobject AzurePipelinesPool", err)
log.Println("Error fetching crdobject AzurePipelinesPool", err)
} else {
log.Println("crdobject AzurePipelinesPool fetched successfully \n", crdobject)
log.Println("Crdobject AzurePipelinesPool fetched successfully \n", crdobject)
}
labels := GenerateLabelsForPod(agentRequest.AgentId)
log.Println("Add an agent Pod using CRD")
if isTestingEnv() {
// currently linux is hardcoded as agentrequest doesnt support demand right now
pod = crdclient.AzurePipelinesPool(podnamespace).AddNewPodForCRTest(crdobject, labels, "linux")
} else {
pod = crdclient.AzurePipelinesPool(podnamespace).AddNewPodForCR(crdobject, labels, "linux")
}
pod = crdclient.AzurePipelinesPool(podnamespace).AddNewPodForCR(crdobject, labels, "linux")
log.Println("pod created ", pod)
log.Println("Agent pod spec fetched ", pod)
cs := CreateClientSet()
@ -70,22 +60,22 @@ func CreatePod(agentRequest AgentRequest, podnamespace string) AgentProvisionRes
var response AgentProvisionResponse
podClient := cs.clientset.CoreV1().Pods(podnamespace)
webserverpod, _ := podClient.List(metav1.ListOptions{LabelSelector: "app=azurepipelinespool-operator"})
webserverpod, webserverpoderr := podClient.List(metav1.ListOptions{LabelSelector: "app=azurepipelinespool-operator"})
if webserverpod.Items != nil {
if webserverpoderr == nil && webserverpod.Items != nil {
AddOwnerRefToObject(pod, AsOwner(&webserverpod.Items[0]))
log.Println("Webserver pod added as owner reference to agent pod ")
log.Println("create secret called")
log.Println("Creating the agent secret")
sec = createSecret(cs, agentRequest, &webserverpod.Items[0])
} else {
sec = createSecret(cs, agentRequest, nil)
log.Println("Web Server Pod not found")
}
// Mount the secrets as a volume
pod.Spec.Volumes = append(pod.Spec.Volumes, *getSecretVolume(sec.Name))
log.Println("secrets mounted as volume")
log.Println("Secrets mounted as volume")
_, err2 := podClient.Create(pod)
if err2 != nil {
@ -181,7 +171,7 @@ func getAgentSecret() *v1.Secret {
func createSecret(cs *k8s, request AgentRequest, m *v1.Pod) *v1.Secret {
secret := getAgentSecret()
log.Println("parsing secret data from agent request")
log.Println("Parsing secret data from agent request")
agentSettings, _ := json.Marshal(request.AgentConfiguration.AgentSettings)
agentCredentials, _ := json.Marshal(request.AgentConfiguration.AgentCredentials)

Просмотреть файл

@ -1,13 +1,8 @@
package main
import (
"io/ioutil"
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/ghodss/yaml"
)
const testnamespace = "azuredevops"
@ -166,9 +161,7 @@ func TestGetBuildPodShouldReturnEmptyStringIfNoBuildKitPodPresent(t *testing.T)
func TestGetBuildPodShouldReturnBuildKitPodNameIfPresent(t *testing.T) {
var agentrequest AgentRequest
agentrequest.AgentId = "1"
SetTestingEnvironmentVariables()
CreateDummyBuildKitPod()
SetTestingEnvironmentVariables(true)
testGetBuildpod := GetBuildKitPod("test", testnamespace)
if testGetBuildpod.Message == "" {
@ -176,21 +169,3 @@ func TestGetBuildPodShouldReturnBuildKitPodNameIfPresent(t *testing.T) {
}
}
func CreateDummyBuildKitPod() {
cs := CreateClientSet()
var buildkitpod v1.Pod
podname := "azure-pipelines-agent"
dat, _ := ioutil.ReadFile("agentpods/" + podname + ".yaml")
var podYaml = string(dat)
_ = yaml.Unmarshal([]byte(podYaml), &buildkitpod)
buildkitpod.SetLabels(map[string]string{
"role": "buildkit",
})
buildkitpod.ObjectMeta.Name = "buildkitd-0"
podClient := cs.clientset.CoreV1().Pods("azuredevops")
_, _ = podClient.Create(&buildkitpod)
}

Просмотреть файл

@ -2,6 +2,7 @@ package v1alpha1
import (
"log"
"os"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -22,7 +23,6 @@ type AzurePipelinesPoolV1Alpha1Client struct {
type AzurePipelinesPoolInterface interface {
Get(name string) (*AzurePipelinesPool, error)
AddNewPodForCR(obj *AzurePipelinesPool, labels map[string]string, poolName string) *v1.Pod
AddNewPodForCRTest(obj *AzurePipelinesPool, labels map[string]string, poolName string) *v1.Pod
}
type AzurePipelinesPoolclient struct {
@ -41,11 +41,28 @@ func (c *AzurePipelinesPoolclient) Get(name string) (*AzurePipelinesPool, error)
func (c *AzurePipelinesPoolclient) AddNewPodForCR(obj *AzurePipelinesPool, labels map[string]string, poolname string) *v1.Pod {
spec := FetchPodSpec(obj, poolname)
var spec *v1.PodSpec
if IsTestingEnv() {
spec = &v1.PodSpec{
Containers: []v1.Container{
{
Name: "vsts-agent",
Image: "prebansa/myagent:v1",
},
},
}
} else {
spec = FetchPodSpec(obj, poolname)
}
// append the RUNNING_ON environment variable
if spec != nil && len(spec.Containers) > 0 {
spec.Containers[0].Env = append(spec.Containers[0].Env, *GetRunningOnEnvironmentVariable())
}
// check if VolumeMounts is not present in the spec; then add the default one
if spec != nil && len(spec.Containers) > 0 && spec.Containers[0].VolumeMounts == nil {
spec.Containers[0].VolumeMounts = append(spec.Containers[0].VolumeMounts, *getDefaultVolumeMount())
spec.Containers[0].VolumeMounts = append(spec.Containers[0].VolumeMounts, *GetDefaultVolumeMount())
}
if spec != nil {
@ -56,31 +73,14 @@ func (c *AzurePipelinesPoolclient) AddNewPodForCR(obj *AzurePipelinesPool, label
},
Spec: *spec,
}
if IsTestingEnv() {
dep.Name = "TestAgentPod"
}
return dep
}
return nil
}
func (c *AzurePipelinesPoolclient) AddNewPodForCRTest(obj *AzurePipelinesPool, labels map[string]string, poolname string) *v1.Pod {
dep := &v1.Pod{
ObjectMeta: meta_v1.ObjectMeta{
Labels: labels,
GenerateName: "azure-pipelines-agent-",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "vsts-agent",
Image: "prebansa/myagent:v1",
},
},
},
}
return dep
}
func FetchPodSpec(obj *AzurePipelinesPool, poolname string) *v1.PodSpec {
if obj.Spec.AgentPools != nil {
@ -94,7 +94,7 @@ func FetchPodSpec(obj *AzurePipelinesPool, poolname string) *v1.PodSpec {
return nil
}
func getDefaultVolumeMount() *v1.VolumeMount {
func GetDefaultVolumeMount() *v1.VolumeMount {
return &v1.VolumeMount{
Name: "agent-creds",
@ -103,3 +103,24 @@ func getDefaultVolumeMount() *v1.VolumeMount {
}
}
func GetRunningOnEnvironmentVariable() *v1.EnvVar {
return &v1.EnvVar{
Name: "RUNNING_ON",
ValueFrom: &v1.EnvVarSource{
ConfigMapKeyRef: &v1.ConfigMapKeySelector{
LocalObjectReference: v1.LocalObjectReference{Name: "kubernetes-config"},
Key: "type",
},
},
}
}
func IsTestingEnv() bool {
testingMode := os.Getenv("IS_TESTENVIRONMENT")
if testingMode == "true" {
return true
}
return false
}

Просмотреть файл

@ -37,6 +37,10 @@ func addKnownTypes(scheme *runtime.Scheme) error {
}
func NewClient(cfg *rest.Config) (*AzurePipelinesPoolV1Alpha1Client, error) {
if IsTestingEnv() {
return &testingclient, nil
}
scheme := runtime.NewScheme()
SchemeBuilder := runtime.NewSchemeBuilder(addKnownTypes)
if err := SchemeBuilder.AddToScheme(scheme); err != nil {
@ -52,30 +56,15 @@ func NewClient(cfg *rest.Config) (*AzurePipelinesPoolV1Alpha1Client, error) {
if err != nil {
return nil, err
}
log.Println("rest client inside newclient", client)
log.Println("Dynamic rest client value - ", client)
return &AzurePipelinesPoolV1Alpha1Client{RestClient: client}, nil
}
func NewClientTest() (*AzurePipelinesPoolV1Alpha1Client, error) {
/*scheme := runtime.NewScheme()
SchemeBuilder := runtime.NewSchemeBuilder(addKnownTypes)
if err := SchemeBuilder.AddToScheme(scheme); err != nil {
return nil, err
}
client, err := rest.RESTClientFor(&rest.Config{APIPath: "/apis", ContentConfig: rest.ContentConfig{GroupVersion: &SchemeGroupVersion, NegotiatedSerializer: serializer.NewCodecFactory(scheme)}})
if err != nil {
return nil, err
}
log.Println("rest client inside newclient", client)*/
return &testingclient, nil
}
func SetClient(s *runtime.Scheme ) {
client, err := rest.RESTClientFor(&rest.Config{APIPath: "/apis", ContentConfig: rest.ContentConfig{GroupVersion: &SchemeGroupVersion, NegotiatedSerializer: serializer.NewCodecFactory(s)}})
if err != nil {
}
log.Println("rest client inside newclient", client)
testingclient.RestClient = client
//testingclient.RestClient = s
}