From c60b0255f19dc99475761afe7221c4849aeab841 Mon Sep 17 00:00:00 2001 From: david kydd Date: Mon, 21 Jun 2021 10:37:04 +1200 Subject: [PATCH 1/2] add default .gitattributes --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..4cab1f4d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Set the default behavior, in case people don't have core.autocrlf set. +* text=auto From d6fc55341a77fe3036698f0c69103fb592151667 Mon Sep 17 00:00:00 2001 From: david kydd Date: Mon, 21 Jun 2021 10:44:56 +1200 Subject: [PATCH 2/2] git add --renormalize --- .gitignore | 666 +++++++++++++++++------------------ CODE_OF_CONDUCT.md | 18 +- LICENSE | 42 +-- builder/Dockerfile | 34 +- pkg/utils/helper.go | 822 ++++++++++++++++++++++---------------------- 5 files changed, 791 insertions(+), 791 deletions(-) diff --git a/.gitignore b/.gitignore index 45f84690..0a74d19f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,333 +1,333 @@ -# Build output -/aks-periscope - -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore - -# User-specific files -*.suo -*.user -*.userosscache -*.sln.docstates - -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -bld/ -[Bb]in/ -[Oo]bj/ -[Ll]og/ - -# Visual Studio 2015/2017 cache/options directory -.vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# Visual Studio 2017 auto generated files -Generated\ Files/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUNIT -*.VisualState.xml -TestResult.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# Benchmark Results -BenchmarkDotNet.Artifacts/ - -# .NET Core -project.lock.json -project.fragment.lock.json -artifacts/ -**/Properties/launchSettings.json - -# StyleCop -StyleCopReport.xml - -# Files built by Visual Studio -*_i.c -*_p.c -*_i.h -*.ilk -*.meta -*.obj -*.iobj -*.pch -*.pdb -*.ipdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb - -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap - -# Visual Studio Trace Files -*.e2e - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# JustCode is a .NET coding add-in -.JustCode - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# AxoCover is a Code Coverage Tool -.axoCover/* -!.axoCover/settings.json - -# Visual Studio code coverage results -*.coverage -*.coveragexml - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# Note: Comment the next line if you want to checkin your web deploy settings, -# but database connection strings (with potential passwords) will be unencrypted -*.pubxml -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# The packages folder can be ignored because of Package Restore -**/[Pp]ackages/* -# except build/, which is used as an MSBuild target. -!**/[Pp]ackages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/[Pp]ackages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt -*.appx - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -orleans.codegen.cs - -# Including strong name files can present a security risk -# (https://github.com/github/gitignore/pull/2483#issue-259490424) -#*.snk - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm -ServiceFabricBackup/ -*.rptproj.bak - -# SQL Server files -*.mdf -*.ldf -*.ndf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings -*.rptproj.rsuser - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat -node_modules/ - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) -*.vbw - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# JetBrains Rider -.idea/ -*.sln.iml - -# CodeRush -.cr/ - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Tabs Studio -*.tss - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs -*.btm.cs -*.odx.cs -*.xsd.cs - -# OpenCover UI analysis results -OpenCover/ - -# Azure Stream Analytics local run output -ASALocalRun/ - -# MSBuild Binary and Structured Log -*.binlog - -# NVidia Nsight GPU debugger configuration file -*.nvuser - -# MFractors (Xamarin productivity tool) working folder -.mfractor/ +# Build output +/aks-periscope + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index c72a5749..f9ba8cf6 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -1,9 +1,9 @@ -# Microsoft Open Source Code of Conduct - -This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). - -Resources: - -- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) -- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) -- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns +# Microsoft Open Source Code of Conduct + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). + +Resources: + +- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) +- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) +- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns diff --git a/LICENSE b/LICENSE index 3d8b93bc..9e841e7a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ - MIT License - - Copyright (c) Microsoft Corporation. - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE + MIT License + + Copyright (c) Microsoft Corporation. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE diff --git a/builder/Dockerfile b/builder/Dockerfile index 876ec031..1f0545af 100644 --- a/builder/Dockerfile +++ b/builder/Dockerfile @@ -1,17 +1,17 @@ -FROM golang AS builder -RUN mkdir /app -ADD . /app -WORKDIR /app -RUN CGO_ENABLED=0 GOOS=linux go build github.com/Azure/aks-periscope/cmd/aks-periscope - -FROM alpine -RUN apk --no-cache add ca-certificates curl openssl bash -ADD https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/linux/amd64/kubectl /usr/local/bin/kubectl -RUN chmod +x /usr/local/bin/kubectl -RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 \ - && chmod +x get_helm.sh \ - && ./get_helm.sh -RUN mkdir /app -WORKDIR /app -COPY --from=builder /app/aks-periscope . -CMD ["./aks-periscope"] +FROM golang AS builder +RUN mkdir /app +ADD . /app +WORKDIR /app +RUN CGO_ENABLED=0 GOOS=linux go build github.com/Azure/aks-periscope/cmd/aks-periscope + +FROM alpine +RUN apk --no-cache add ca-certificates curl openssl bash +ADD https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/linux/amd64/kubectl /usr/local/bin/kubectl +RUN chmod +x /usr/local/bin/kubectl +RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 \ + && chmod +x get_helm.sh \ + && ./get_helm.sh +RUN mkdir /app +WORKDIR /app +COPY --from=builder /app/aks-periscope . +CMD ["./aks-periscope"] diff --git a/pkg/utils/helper.go b/pkg/utils/helper.go index 7456aaf0..f113f7f9 100644 --- a/pkg/utils/helper.go +++ b/pkg/utils/helper.go @@ -1,411 +1,411 @@ -package utils - -import ( - "bytes" - "encoding/json" - "errors" - "fmt" - "io/ioutil" - "log" - "net/http" - "os" - "os/exec" - "path/filepath" - "strings" - "time" -) - -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"` -} - -type CommandOutputStreams struct { - Stdout string - Stderr string -} - -// 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") - if err != nil { - return "", fmt.Errorf("Fail to get host name: %+v", err) - } - - return strings.TrimSuffix(string(hostname), "\n"), 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"} - args = append(args, "--") - args = append(args, command) - args = append(args, arg...) - - cmd := exec.Command("nsenter", args...) - out, err := cmd.CombinedOutput() - if err != nil { - return "", fmt.Errorf("Fail to run command on host: %+v", err) - } - - return string(out), nil -} - -// RunCommandOnContainerWithOutputStreams runs a command on container system and returns both the stdout and stderr output streams -func RunCommandOnContainerWithOutputStreams(command string, arg ...string) (CommandOutputStreams, error) { - cmd := exec.Command(command, arg...) - - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err := cmd.Run() - outputStreams := CommandOutputStreams{stdout.String(), stderr.String()} - - if err != nil { - return outputStreams, fmt.Errorf("Fail to run command in container: %s", fmt.Sprint(err)+": "+stderr.String()) - } - - return outputStreams, nil -} - -// RunCommandOnContainer runs a command on container system and returns the stdout output stream -func RunCommandOnContainer(command string, arg ...string) (string, error) { - outputStreams, err := RunCommandOnContainerWithOutputStreams(command, arg...) - return outputStreams.Stdout, err -} - -// RunBackgroundCommand starts running a command on a container system in the background and returns its process ID -func RunBackgroundCommand(command string, arg ...string) (int, error) { - cmd := exec.Command(command, arg...) - var out bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &out - cmd.Stderr = &stderr - err := cmd.Start() - if err != nil { - return 0, fmt.Errorf("Start background command in container exited with message %s: %w", stderr.String(), err) - } - return cmd.Process.Pid, nil -} - -// Finds and kills a process with a given process ID -func KillProcess(pid int) error { - process, err := os.FindProcess(pid) - if err != nil { - return fmt.Errorf("Find process with pid %d to kill: %w", pid, err) - } - if err := process.Kill(); err != nil { - return err - } - return nil -} - -// Tries to issue an HTTP GET request up to maxRetries times -func GetUrlWithRetries(url string, maxRetries int) ([]byte, error) { - retry := 1 - for { - resp, err := http.Get(url) - if err != nil { - if retry == maxRetries { - return nil, fmt.Errorf("Max retries reached for request HTTP Get %s: %w", url, err) - } - retry++ - time.Sleep(5 * time.Second) - } else { - defer resp.Body.Close() - return ioutil.ReadAll(resp.Body) - } - } -} - -// WriteToFile writes data to a file -func WriteToFile(fileName string, data string) error { - if err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm); err != nil { - return fmt.Errorf("Fail to create path directories for file %s: %w", fileName, err) - } - f, err := os.Create(fileName) - if err != nil { - return fmt.Errorf("Fail to create file %s: %+v", fileName, err) - } - defer f.Close() - - _, err = f.Write([]byte(data)) - if err != nil { - return fmt.Errorf("Fail to write data to file %s: %+v", fileName, err) - } - - return nil -} - -// CreateCollectorDir creates a working dir for a collector -func CreateCollectorDir(name string) (string, error) { - hostName, err := GetHostName() - if err != nil { - return "", err - } - - creationTimeStamp, err := GetCreationTimeStamp() - if err != nil { - return "", err - } - - rootPath := filepath.Join("/aks-periscope", strings.Replace(creationTimeStamp, ":", "-", -1), hostName, "collector", name) - err = os.MkdirAll(rootPath, os.ModePerm) - if err != nil { - return "", fmt.Errorf("Fail to create dir %s: %+v", rootPath, err) - } - - return rootPath, nil -} - -// CreateDiagnosticDir creates a working dir for diagnostic -func CreateDiagnosticDir() (string, error) { - hostName, err := GetHostName() - if err != nil { - return "", err - } - - creationTimeStamp, err := GetCreationTimeStamp() - if err != nil { - return "", err - } - - rootPath := filepath.Join("/aks-periscope", strings.Replace(creationTimeStamp, ":", "-", -1), hostName, "diagnoser") - err = os.MkdirAll(rootPath, os.ModePerm) - if err != nil { - return "", fmt.Errorf("Fail to create dir %s: %+v", rootPath, err) - } - - return rootPath, nil -} - -// CreateKubeConfigFromServiceAccount creates kubeconfig based on creds in service account -func CreateKubeConfigFromServiceAccount() error { - token, err := RunCommandOnContainer("cat", "/var/run/secrets/kubernetes.io/serviceaccount/token") - if err != nil { - return err - } - - _, err = RunCommandOnContainer("kubectl", "config", "set-credentials", "aks-periscope-service-account", "--token="+token) - if err != nil { - return err - } - - _, err = RunCommandOnContainer("kubectl", "config", "set-cluster", "aks-periscope-cluster", "--server=https://kubernetes.default.svc.cluster.local:443", "--certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt") - if err != nil { - return err - } - - _, err = RunCommandOnContainer("kubectl", "config", "set-context", "aks-periscope-context", "--user=aks-periscope-service-account", "--cluster=aks-periscope-cluster") - if err != nil { - return err - } - - _, err = RunCommandOnContainer("kubectl", "config", "use-context", "aks-periscope-context") - if err != nil { - return err - } - - return nil -} - -// GetCreationTimeStamp returns a create timestamp -func GetCreationTimeStamp() (string, error) { - creationTimeStamp, err := RunCommandOnContainer("kubectl", "get", "pods", "--all-namespaces", "-l", "app=aks-periscope", "-o", "jsonpath=\"{.items[0].metadata.creationTimestamp}\"") - if err != nil { - return "", err - } - - return creationTimeStamp[1 : len(creationTimeStamp)-1], nil -} - -// WriteToCRD writes diagnostic data to CRD -func WriteToCRD(fileName string, key string) error { - hostName, err := GetHostName() - if err != nil { - return err - } - - crdName := "aks-periscope-diagnostic" + "-" + hostName - - jsonBytes, err := ioutil.ReadFile(fileName) - if err != nil { - return err - } - - patchContent := fmt.Sprintf("{\"spec\":{%q:%q}}", key, string(jsonBytes)) - - _, err = RunCommandOnContainer("kubectl", "-n", "aks-periscope", "patch", "apd", crdName, "-p", patchContent, "--type=merge") - if err != nil { - return err - } - - return nil -} - -// CreateCRD creates a CRD object -func CreateCRD() error { - hostName, err := GetHostName() - if err != nil { - return err - } - - crdName := "aks-periscope-diagnostic" + "-" + hostName - - if err = writeDiagnosticCRD(crdName); err != nil { - return err - } - - _, err = RunCommandOnContainer("kubectl", "apply", "-f", "aks-periscope-diagnostic-crd.yaml") - if err != nil { - return err - } - - return nil -} - -// GetResourceList gets a list of all resources of given type in a specified namespace -func GetResourceList(kubeCmds []string, separator string) ([]string, error) { - outputStreams, err := RunCommandOnContainerWithOutputStreams("kubectl", kubeCmds...) - - if err != nil { - return nil, err - } - - resourceList := outputStreams.Stdout - // If the resource is not found within the cluster, then log a message and do not return any resources. - if len(resourceList) == 0 { - return nil, fmt.Errorf("No '%s' resource found in the cluster for given kubectl command", kubeCmds[1]) - } - - return strings.Split(strings.Trim(resourceList, "\""), separator), nil -} - -func writeDiagnosticCRD(crdName string) error { - f, err := os.Create("aks-periscope-diagnostic-crd.yaml") - if err != nil { - return err - } - defer f.Close() - - _, err = f.WriteString("apiVersion: \"aks-periscope.azure.github.com/v1\"\n") - if err != nil { - return err - } - - _, err = f.WriteString("kind: Diagnostic\n") - if err != nil { - return err - } - - _, err = f.WriteString("metadata:\n") - if err != nil { - return err - } - - _, err = f.WriteString(" name: " + crdName + "\n") - if err != nil { - return err - } - - _, err = f.WriteString(" namespace: aks-periscope\n") - if err != nil { - return err - } - - _, err = f.WriteString("spec:\n") - if err != nil { - return err - } - - _, err = f.WriteString(" networkconfig: \"\"\n") - if err != nil { - return err - } - - _, err = f.WriteString(" networkoutbound: \"\"\n") - if err != nil { - return err - } - - return nil -} +package utils + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "os/exec" + "path/filepath" + "strings" + "time" +) + +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"` +} + +type CommandOutputStreams struct { + Stdout string + Stderr string +} + +// 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") + if err != nil { + return "", fmt.Errorf("Fail to get host name: %+v", err) + } + + return strings.TrimSuffix(string(hostname), "\n"), 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"} + args = append(args, "--") + args = append(args, command) + args = append(args, arg...) + + cmd := exec.Command("nsenter", args...) + out, err := cmd.CombinedOutput() + if err != nil { + return "", fmt.Errorf("Fail to run command on host: %+v", err) + } + + return string(out), nil +} + +// RunCommandOnContainerWithOutputStreams runs a command on container system and returns both the stdout and stderr output streams +func RunCommandOnContainerWithOutputStreams(command string, arg ...string) (CommandOutputStreams, error) { + cmd := exec.Command(command, arg...) + + var stdout bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + err := cmd.Run() + outputStreams := CommandOutputStreams{stdout.String(), stderr.String()} + + if err != nil { + return outputStreams, fmt.Errorf("Fail to run command in container: %s", fmt.Sprint(err)+": "+stderr.String()) + } + + return outputStreams, nil +} + +// RunCommandOnContainer runs a command on container system and returns the stdout output stream +func RunCommandOnContainer(command string, arg ...string) (string, error) { + outputStreams, err := RunCommandOnContainerWithOutputStreams(command, arg...) + return outputStreams.Stdout, err +} + +// RunBackgroundCommand starts running a command on a container system in the background and returns its process ID +func RunBackgroundCommand(command string, arg ...string) (int, error) { + cmd := exec.Command(command, arg...) + var out bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &stderr + err := cmd.Start() + if err != nil { + return 0, fmt.Errorf("Start background command in container exited with message %s: %w", stderr.String(), err) + } + return cmd.Process.Pid, nil +} + +// Finds and kills a process with a given process ID +func KillProcess(pid int) error { + process, err := os.FindProcess(pid) + if err != nil { + return fmt.Errorf("Find process with pid %d to kill: %w", pid, err) + } + if err := process.Kill(); err != nil { + return err + } + return nil +} + +// Tries to issue an HTTP GET request up to maxRetries times +func GetUrlWithRetries(url string, maxRetries int) ([]byte, error) { + retry := 1 + for { + resp, err := http.Get(url) + if err != nil { + if retry == maxRetries { + return nil, fmt.Errorf("Max retries reached for request HTTP Get %s: %w", url, err) + } + retry++ + time.Sleep(5 * time.Second) + } else { + defer resp.Body.Close() + return ioutil.ReadAll(resp.Body) + } + } +} + +// WriteToFile writes data to a file +func WriteToFile(fileName string, data string) error { + if err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm); err != nil { + return fmt.Errorf("Fail to create path directories for file %s: %w", fileName, err) + } + f, err := os.Create(fileName) + if err != nil { + return fmt.Errorf("Fail to create file %s: %+v", fileName, err) + } + defer f.Close() + + _, err = f.Write([]byte(data)) + if err != nil { + return fmt.Errorf("Fail to write data to file %s: %+v", fileName, err) + } + + return nil +} + +// CreateCollectorDir creates a working dir for a collector +func CreateCollectorDir(name string) (string, error) { + hostName, err := GetHostName() + if err != nil { + return "", err + } + + creationTimeStamp, err := GetCreationTimeStamp() + if err != nil { + return "", err + } + + rootPath := filepath.Join("/aks-periscope", strings.Replace(creationTimeStamp, ":", "-", -1), hostName, "collector", name) + err = os.MkdirAll(rootPath, os.ModePerm) + if err != nil { + return "", fmt.Errorf("Fail to create dir %s: %+v", rootPath, err) + } + + return rootPath, nil +} + +// CreateDiagnosticDir creates a working dir for diagnostic +func CreateDiagnosticDir() (string, error) { + hostName, err := GetHostName() + if err != nil { + return "", err + } + + creationTimeStamp, err := GetCreationTimeStamp() + if err != nil { + return "", err + } + + rootPath := filepath.Join("/aks-periscope", strings.Replace(creationTimeStamp, ":", "-", -1), hostName, "diagnoser") + err = os.MkdirAll(rootPath, os.ModePerm) + if err != nil { + return "", fmt.Errorf("Fail to create dir %s: %+v", rootPath, err) + } + + return rootPath, nil +} + +// CreateKubeConfigFromServiceAccount creates kubeconfig based on creds in service account +func CreateKubeConfigFromServiceAccount() error { + token, err := RunCommandOnContainer("cat", "/var/run/secrets/kubernetes.io/serviceaccount/token") + if err != nil { + return err + } + + _, err = RunCommandOnContainer("kubectl", "config", "set-credentials", "aks-periscope-service-account", "--token="+token) + if err != nil { + return err + } + + _, err = RunCommandOnContainer("kubectl", "config", "set-cluster", "aks-periscope-cluster", "--server=https://kubernetes.default.svc.cluster.local:443", "--certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt") + if err != nil { + return err + } + + _, err = RunCommandOnContainer("kubectl", "config", "set-context", "aks-periscope-context", "--user=aks-periscope-service-account", "--cluster=aks-periscope-cluster") + if err != nil { + return err + } + + _, err = RunCommandOnContainer("kubectl", "config", "use-context", "aks-periscope-context") + if err != nil { + return err + } + + return nil +} + +// GetCreationTimeStamp returns a create timestamp +func GetCreationTimeStamp() (string, error) { + creationTimeStamp, err := RunCommandOnContainer("kubectl", "get", "pods", "--all-namespaces", "-l", "app=aks-periscope", "-o", "jsonpath=\"{.items[0].metadata.creationTimestamp}\"") + if err != nil { + return "", err + } + + return creationTimeStamp[1 : len(creationTimeStamp)-1], nil +} + +// WriteToCRD writes diagnostic data to CRD +func WriteToCRD(fileName string, key string) error { + hostName, err := GetHostName() + if err != nil { + return err + } + + crdName := "aks-periscope-diagnostic" + "-" + hostName + + jsonBytes, err := ioutil.ReadFile(fileName) + if err != nil { + return err + } + + patchContent := fmt.Sprintf("{\"spec\":{%q:%q}}", key, string(jsonBytes)) + + _, err = RunCommandOnContainer("kubectl", "-n", "aks-periscope", "patch", "apd", crdName, "-p", patchContent, "--type=merge") + if err != nil { + return err + } + + return nil +} + +// CreateCRD creates a CRD object +func CreateCRD() error { + hostName, err := GetHostName() + if err != nil { + return err + } + + crdName := "aks-periscope-diagnostic" + "-" + hostName + + if err = writeDiagnosticCRD(crdName); err != nil { + return err + } + + _, err = RunCommandOnContainer("kubectl", "apply", "-f", "aks-periscope-diagnostic-crd.yaml") + if err != nil { + return err + } + + return nil +} + +// GetResourceList gets a list of all resources of given type in a specified namespace +func GetResourceList(kubeCmds []string, separator string) ([]string, error) { + outputStreams, err := RunCommandOnContainerWithOutputStreams("kubectl", kubeCmds...) + + if err != nil { + return nil, err + } + + resourceList := outputStreams.Stdout + // If the resource is not found within the cluster, then log a message and do not return any resources. + if len(resourceList) == 0 { + return nil, fmt.Errorf("No '%s' resource found in the cluster for given kubectl command", kubeCmds[1]) + } + + return strings.Split(strings.Trim(resourceList, "\""), separator), nil +} + +func writeDiagnosticCRD(crdName string) error { + f, err := os.Create("aks-periscope-diagnostic-crd.yaml") + if err != nil { + return err + } + defer f.Close() + + _, err = f.WriteString("apiVersion: \"aks-periscope.azure.github.com/v1\"\n") + if err != nil { + return err + } + + _, err = f.WriteString("kind: Diagnostic\n") + if err != nil { + return err + } + + _, err = f.WriteString("metadata:\n") + if err != nil { + return err + } + + _, err = f.WriteString(" name: " + crdName + "\n") + if err != nil { + return err + } + + _, err = f.WriteString(" namespace: aks-periscope\n") + if err != nil { + return err + } + + _, err = f.WriteString("spec:\n") + if err != nil { + return err + } + + _, err = f.WriteString(" networkconfig: \"\"\n") + if err != nil { + return err + } + + _, err = f.WriteString(" networkoutbound: \"\"\n") + if err != nil { + return err + } + + return nil +}