Skip to content

Commit

Permalink
Add support for applications on Azure Stack Cloud (Azure#64)
Browse files Browse the repository at this point in the history
Add support for applications on Azure Stack Cloud.
  • Loading branch information
shwetamurali committed Jun 23, 2021
1 parent e7b9fbf commit 3840822
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/aks-periscope/aks-periscope.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func main() {

clusterType := os.Getenv("CLUSTER_TYPE")

// Copies self-signed cert information to container if application is running on Azure Stack Cloud.
// We need the cert in order to communicate with the storage account.
if utils.IsAzureStackCloud() {
if err := utils.CopyFileFromHost("/etc/ssl/certs/azsCertificate.pem", "/etc/ssl/certs/azsCertificate.pem"); err != nil {
log.Fatalf("cannot copy cert for Azure Stack Cloud environment: %v", err)
}
}

collectors := []interfaces.Collector{}
containerLogsCollector := collector.NewContainerLogsCollector(exporter)
networkOutboundCollector := collector.NewNetworkOutboundCollector(5, exporter)
Expand Down
3 changes: 2 additions & 1 deletion pkg/exporter/azureblob_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (exporter *AzureBlobExporter) Export(files []string) error {
accountName := os.Getenv("AZURE_BLOB_ACCOUNT_NAME")
sasKey := os.Getenv("AZURE_BLOB_SAS_KEY")

url, err := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/%s%s", accountName, containerName, sasKey))
ses := utils.GetStorageEndpointSuffix()
url, err := url.Parse(fmt.Sprintf("https://%s.blob.%s/%s%s", accountName, ses, containerName, sasKey))
if err != nil {
return fmt.Errorf("Fail to build blob container url: %+v", err)
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,77 @@ package utils

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

const (
// PublicAzureStorageEndpointSuffix defines default Storage Endpoint Suffix
PublicAzureStorageEndpointSuffix = "core.windows.net"
// AzureStackCloudName references the value that will be under the key "cloud" in azure.json if the application is running on Azure Stack Cloud
// https://kubernetes-sigs.github.io/cloud-provider-azure/install/configs/#azure-stack-configuration -- See this documentation for the well-known cloud name.
AzureStackCloudName = "AzureStackCloud"
)

// Azure defines Azure configuration
type Azure struct {
Cloud string `json:"cloud"`
}

// AzureStackCloud defines Azure Stack Cloud configuration
type AzureStackCloud struct {
StorageEndpointSuffix string `json:"storageEndpointSuffix"`
}

// IsAzureStackCloud returns true if the application is running on Azure Stack Cloud
func IsAzureStackCloud() bool {
azureFile, err := RunCommandOnHost("cat", "/etc/kubernetes/azure.json")
if err != nil {
return false
}
var azure Azure
if err = json.Unmarshal([]byte(azureFile), &azure); err != nil {
return false
}
cloud := azure.Cloud
return strings.EqualFold(cloud, AzureStackCloudName)
}

// CopyFileFromHost saves the specified source file to the destination
func CopyFileFromHost(source, destination string) error {
sourceFile, err := RunCommandOnHost("cat", source)
if err != nil {
return fmt.Errorf("unable to retrieve source content: %w", err)
}
if err = WriteToFile(destination, sourceFile); err != nil {
return fmt.Errorf("unable to write source file to destination: %w", err)
}
return nil
}

// GetStorageEndpointSuffix returns the SES url from the JSON file as a string
func GetStorageEndpointSuffix() string {
if IsAzureStackCloud() {
ascFile, err := RunCommandOnHost("cat", "/etc/kubernetes/azurestackcloud.json")
if err != nil {
log.Fatalf("unable to locate azurestackcloud.json to extract storage endpoint suffix: %v", err)
}
var azurestackcloud AzureStackCloud
if err = json.Unmarshal([]byte(ascFile), &azurestackcloud); err != nil {
log.Fatalf("unable to read azurestackcloud.json file: %v", err)
}
return azurestackcloud.StorageEndpointSuffix
}
return PublicAzureStorageEndpointSuffix
}

// GetHostName get host name
func GetHostName() (string, error) {
hostname, err := RunCommandOnHost("cat", "/etc/hostname")
Expand Down

0 comments on commit 3840822

Please sign in to comment.