Skip to content

Commit

Permalink
Fix: remove hardcoded path to kubeconfig file
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Tincelin committed Aug 12, 2021
1 parent e2a8755 commit 463e94b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 108 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jobs:
run: |
(cd ./deployment && kustomize edit set image aksrepos.azurecr.io/staging/aks-periscope=localhost:5000/periscope:foo)
kubectl apply -f <(kustomize build ./deployment)
kubectl -n aks-periscope describe ds aks-periscope
kubectl -n aks-periscope wait po --all --for condition=ready --timeout=60s
- name: Go tests
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
Expand Down
21 changes: 15 additions & 6 deletions deployment/examples/kustomization_storage_account.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# This is an example file to use an Azure Storage Account to export data
# 3 values are required:
# - AZURE_BLOB_SAS_KEY
# - AZURE_BLOB_ACCOUNT_NAME
# - AZURE_BLOB_CONTAINER_NAME: name of the container where the data will be exported
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: aks-periscope

resources:
- https://github.com/Azure/aks-periscope/blob/master/deployment/kustomization.yaml
- https://github.com/Azure/aks-periscope/deployment//

secretGenerator:
- name: azureblob-secret
literals:
- AZURE_BLOB_SAS_KEY=<base 64 encoded value>

- AZURE_BLOB_SAS_KEY=<?sv=&ss=&srt=&sp=&se=&st=&spr=&sig=>
patches:
- target:
group: apps
Expand All @@ -17,10 +24,12 @@ patches:
version: v1
patch: |-
- op: add
path: '/spec/template/spec/containers/0/env/-'
path: '/spec/template/spec/containers/0/env'
value:
name: AZURE_BLOB_ACCOUNT_NAME
value: <name>
- name: AZURE_BLOB_ACCOUNT_NAME
value: <name>
- name: AZURE_BLOB_CONTAINER_NAME
value: <name>
- target:
group: apps
kind: DaemonSet
Expand Down
50 changes: 0 additions & 50 deletions docs/kustomize.md

This file was deleted.

15 changes: 1 addition & 14 deletions pkg/collector/networkoutbound_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"net"
"time"

"github.com/Azure/aks-periscope/pkg/utils"
)

type networkOutboundType struct {
Expand Down Expand Up @@ -39,11 +37,6 @@ func (collector *NetworkOutboundCollector) GetName() string {

// Collect implements the interface method
func (collector *NetworkOutboundCollector) Collect() error {
APIServerFQDN, err := utils.GetAPIServerFQDN()
if err != nil {
return err
}

outboundTypes := []networkOutboundType{}
outboundTypes = append(outboundTypes,
networkOutboundType{
Expand All @@ -57,12 +50,6 @@ func (collector *NetworkOutboundCollector) Collect() error {
URL: "kubernetes.default.svc.cluster.local:443",
},
)
outboundTypes = append(outboundTypes,
networkOutboundType{
Type: "AKS Tunnel",
URL: APIServerFQDN + ":443",
},
)
outboundTypes = append(outboundTypes,
networkOutboundType{
Type: "Azure Container Registry",
Expand All @@ -78,7 +65,7 @@ func (collector *NetworkOutboundCollector) Collect() error {

for _, outboundType := range outboundTypes {
timeout := time.Duration(5 * time.Second)
_, err = net.DialTimeout("tcp", outboundType.URL, timeout)
_, err := net.DialTimeout("tcp", outboundType.URL, timeout)

status := "Connected"
if err != nil {
Expand Down
21 changes: 6 additions & 15 deletions pkg/exporter/azureblob_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (
"github.com/Azure/azure-storage-blob-go/azblob"
)

const (
maxContainerNameLength = 63
)

// AzureBlobExporter defines an Azure Blob Exporter
type AzureBlobExporter struct {
hostname string
Expand All @@ -33,23 +29,18 @@ func NewAzureBlobExporter(creationTime, hostname string) *AzureBlobExporter {
}

func createContainerURL() (azblob.ContainerURL, error) {
APIServerFQDN, err := utils.GetAPIServerFQDN()
if err != nil {
return azblob.ContainerURL{}, err
}
accountName := os.Getenv("AZURE_BLOB_ACCOUNT_NAME")
sasKey := os.Getenv("AZURE_BLOB_SAS_KEY")
containerName := os.Getenv("AZURE_BLOB_CONTAINER_NAME")

containerName := strings.Replace(APIServerFQDN, ".", "-", -1)
containerLen := strings.Index(containerName, "-hcp-")
if containerLen == -1 {
containerLen = maxContainerNameLength
if accountName == "" || sasKey == "" || containerName == "" {
log.Print("Storage Account information were not provided. Export to Azure Storage Account will be skiped.")
return azblob.ContainerURL{}, nil
}
containerName = strings.TrimRight(containerName[:containerLen], "-")

ctx := context.Background()

pipeline := azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{})
accountName := os.Getenv("AZURE_BLOB_ACCOUNT_NAME")
sasKey := os.Getenv("AZURE_BLOB_SAS_KEY")

ses := utils.GetStorageEndpointSuffix()
url, err := url.Parse(fmt.Sprintf("https://%s.blob.%s/%s%s", accountName, ses, containerName, sasKey))
Expand Down
23 changes: 0 additions & 23 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utils
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -132,28 +131,6 @@ func GetHostName() (string, error) {
return hostName.HostName, nil
}

// GetAPIServerFQDN gets the API Server FQDN from the kubeconfig file
func GetAPIServerFQDN() (string, error) {
output, err := RunCommandOnHost("cat", "/var/lib/kubelet/kubeconfig")

if err != nil {
return "", fmt.Errorf("Can't open kubeconfig file: %+v", err)
}

lines := strings.Split(output, "\n")
for _, line := range lines {
index := strings.Index(line, "server: ")
if index >= 0 {
fqdn := line[index+len("server: "):]
fqdn = strings.Replace(fqdn, "https://", "", -1)
fqdn = strings.Replace(fqdn, ":443", "", -1)
return fqdn, nil
}
}

return "", errors.New("Could not find server definitions in kubeconfig")
}

// RunCommandOnHost runs a command on host system
func RunCommandOnHost(command string, arg ...string) (string, error) {
args := []string{"--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid"}
Expand Down

0 comments on commit 463e94b

Please sign in to comment.