diff --git a/cmd/aks-periscope/aks-periscope.go b/cmd/aks-periscope/aks-periscope.go index 26fe6c17..e9f17cad 100644 --- a/cmd/aks-periscope/aks-periscope.go +++ b/cmd/aks-periscope/aks-periscope.go @@ -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) diff --git a/pkg/exporter/azureblob_exporter.go b/pkg/exporter/azureblob_exporter.go index f74230fa..525fe434 100644 --- a/pkg/exporter/azureblob_exporter.go +++ b/pkg/exporter/azureblob_exporter.go @@ -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) } diff --git a/pkg/utils/helper.go b/pkg/utils/helper.go index 9948ca1a..15dc217a 100644 --- a/pkg/utils/helper.go +++ b/pkg/utils/helper.go @@ -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")