diff --git a/providers/azure/client/aci/client_test.go b/providers/azure/client/aci/client_test.go index bae449f4e..fb918cde5 100644 --- a/providers/azure/client/aci/client_test.go +++ b/providers/azure/client/aci/client_test.go @@ -1,6 +1,9 @@ package aci import ( + "encoding/json" + "fmt" + "io/ioutil" "log" "os" "strings" @@ -360,6 +363,111 @@ func TestCreateContainerGroupWithReadinessProbe(t *testing.T) { } } +func logAnalyticsWorkspaceFromFile(filepath string) (*LogAnalyticsWorkspace, error) { + analyticsdata, err := ioutil.ReadFile(filepath) + if err != nil { + return nil, fmt.Errorf("Reading LogAnalyticsWorkspace file %q failed: %v", filepath, err) + } + // Unmarshal the log analytics file. + var law LogAnalyticsWorkspace + if err := json.Unmarshal(analyticsdata, &law); err != nil { + return nil, err + } + return &law, nil +} + +func TestCreateContainerGroupWithLogAnalytics(t *testing.T) { + law, err := logAnalyticsWorkspaceFromFile("../../../../loganalytics.json") + if err != nil { + t.Fatal(err) + } + cgname := "cgla" + cg, err := client.CreateContainerGroup(resourceGroup, cgname, ContainerGroup{ + Location: location, + ContainerGroupProperties: ContainerGroupProperties{ + OsType: Linux, + Containers: []Container{ + { + Name: "nginx", + ContainerProperties: ContainerProperties{ + Image: "nginx", + Command: []string{"nginx", "-g", "daemon off;"}, + Ports: []ContainerPort{ + { + Protocol: ContainerNetworkProtocolTCP, + Port: 80, + }, + }, + Resources: ResourceRequirements{ + Requests: &ResourceRequests{ + CPU: 1, + MemoryInGB: 1, + }, + Limits: &ResourceLimits{ + CPU: 1, + MemoryInGB: 1, + }, + }, + }, + }, + }, + Diagnostics: &ContainerGroupDiagnostics{ + LogAnalytics: law, + }, + }, + }) + if err != nil { + t.Fatal(err) + } + if cg.Name != cgname { + t.Fatalf("resource group name is %s, expected %s", cg.Name, cgname) + } + if err := client.DeleteContainerGroup(resourceGroup, cgname); err != nil { + t.Fatalf("Delete Container Group failed: %s", err.Error()) + } +} + +func TestCreateContainerGroupWithInvalidLogAnalytics(t *testing.T) { + law := &LogAnalyticsWorkspace{} + _, err := client.CreateContainerGroup(resourceGroup, containerGroup, ContainerGroup{ + Location: location, + ContainerGroupProperties: ContainerGroupProperties{ + OsType: Linux, + Containers: []Container{ + { + Name: "nginx", + ContainerProperties: ContainerProperties{ + Image: "nginx", + Command: []string{"nginx", "-g", "daemon off;"}, + Ports: []ContainerPort{ + { + Protocol: ContainerNetworkProtocolTCP, + Port: 80, + }, + }, + Resources: ResourceRequirements{ + Requests: &ResourceRequests{ + CPU: 1, + MemoryInGB: 1, + }, + Limits: &ResourceLimits{ + CPU: 1, + MemoryInGB: 1, + }, + }, + }, + }, + }, + Diagnostics: &ContainerGroupDiagnostics{ + LogAnalytics: law, + }, + }, + }) + if err == nil { + t.Fatal("TestCreateContainerGroupWithInvalidLogAnalytics should fail but encountered no errors") + } +} + func TestDeleteContainerGroup(t *testing.T) { err := client.DeleteContainerGroup(resourceGroup, containerGroup) if err != nil { diff --git a/providers/azure/client/aci/types.go b/providers/azure/client/aci/types.go index 126bce8b5..1407c57e7 100644 --- a/providers/azure/client/aci/types.go +++ b/providers/azure/client/aci/types.go @@ -91,6 +91,7 @@ type ContainerGroupProperties struct { OsType OperatingSystemTypes `json:"osType,omitempty"` Volumes []Volume `json:"volumes,omitempty"` InstanceView ContainerGroupPropertiesInstanceView `json:"instanceView,omitempty"` + Diagnostics *ContainerGroupDiagnostics `json:"diagnostics,omitempty"` } // ContainerGroupPropertiesInstanceView is the instance view of the container group. Only valid in response. @@ -320,3 +321,14 @@ type ContainerHTTPGetProbe struct { Path string `json:"path,omitempty"` Scheme string `json:"scheme,omitempty"` } + +// ContainerGroupDiagnostics contains an instance of LogAnalyticsWorkspace +type ContainerGroupDiagnostics struct { + LogAnalytics *LogAnalyticsWorkspace `json:"loganalytics,omitempty"` +} + +// LogAnalyticsWorkspace defines details for a Log Analytics workspace +type LogAnalyticsWorkspace struct { + WorkspaceID string `json:"workspaceID,omitempty"` + WorkspaceKey string `json:"workspaceKey,omitempty"` +} diff --git a/scripts/createCredentials.sh b/scripts/createCredentials.sh index 4e412ee50..54ce8c591 100644 --- a/scripts/createCredentials.sh +++ b/scripts/createCredentials.sh @@ -15,6 +15,14 @@ cat < ${outputPathCredsfile} } EOF +# This will build the log analytics credentials during CI +cat < ${outputPathLogAnalyticsFile} +{ + "workspaceID": "$omsworkspaceID", + "workspaceKey": "$omsworkspaceKey" +} +EOF + # This will build the kubeConfig during the CI cat < ${outputPathKubeConfigFile} ---