Skip to content

Commit

Permalink
Add ClusterName and Region to response (#286)
Browse files Browse the repository at this point in the history
* Add ClusterName and Region to response

* Upgrade golangci-lint to 1.52.2 and address issues

* Upgrade setup-protoc and use token for rate limiting
  • Loading branch information
Smirl committed Apr 11, 2023
1 parent 62935a2 commit 08b78a9
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 112 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ jobs:
uses: docker/setup-buildx-action@v2

- name: Install Protoc
uses: arduino/setup-protoc@master
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Get protoc go binaries
run: make tools
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
languages: go

- name: Install Protoc
uses: arduino/setup-protoc@master
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Get protoc go binaries
run: make tools
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
uses: actions/checkout@v3

- name: Install Protoc
uses: arduino/setup-protoc@master
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Get protoc go binaries
run: make tools
Expand All @@ -33,5 +35,5 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.45.2
version: v1.52.2
args: --timeout 5m
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
uses: docker/setup-buildx-action@v2

- name: Install Protoc
uses: arduino/setup-protoc@master
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Get protoc go binaries
run: make tools
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/golangci/golangci-lint
rev: v1.42.0
rev: v1.52.2
hooks:
- id: golangci-lint
- repo: https://github.com/gruntwork-io/pre-commit
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ require (
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
91 changes: 4 additions & 87 deletions go.sum

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions internal/helpers/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package helpers

import "os"

// GetEnv retrieves an environment variable with a default value
// GetEnv retrieves list of possible environment variables with a default value
// if the environment variable is missing
func GetEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
func GetEnv(keysAndFallback ...string) string {
last := len(keysAndFallback) - 1
for _, key := range keysAndFallback[:last] {
if value, ok := os.LookupEnv(key); ok {
return value
}
}
return fallback
return keysAndFallback[last]
}
26 changes: 23 additions & 3 deletions internal/helpers/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,36 @@ import (
func TestGetEnv(t *testing.T) {
expected := "120"
os.Setenv("MAX_DELAY", expected)
defer os.Unsetenv("MAX_DELAY")
value := GetEnv("MAX_DELAY", "12")
if value != expected {
t.Errorf("GetEnv returned unexpected value: got %v want %v",
value, expected)
t.Errorf("GetEnv returned unexpected value: got %v want %v", value, expected)
}
}

func TestGetEnvDefault(t *testing.T) {
defaultValue := "42"
value = GetEnv("MISSING", defaultValue)
value := GetEnv("MISSING", defaultValue)
if value != defaultValue {
t.Errorf("GetEnv returned unexpected value: got %v want %v",
value, defaultValue)
}
}

func TestGetEnvMany(t *testing.T) {
expected := "35"
os.Setenv("SECOND", expected)
defer os.Unsetenv("SECOND")
value := GetEnv("MISSING", "SECOND", "")
if value != expected {
t.Errorf("GetEnv returned unexpected value: got %v want %v", value, expected)
}
}

func TestGetEnvManyDefault(t *testing.T) {
defaultValue := "42"
value := GetEnv("MISSING", "SECOND", defaultValue)
if value != defaultValue {
t.Errorf("GetEnv returned unexpected value: got %v want %v", value, defaultValue)
}
}
4 changes: 3 additions & 1 deletion pkg/infrabin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package infrabin

import (
"fmt"
"strings"
"time"

"github.com/spf13/viper"
Expand Down Expand Up @@ -38,6 +39,8 @@ func ReadConfiguration() {
// Config file should be config.yaml
viper.SetConfigName(DefaultConfigName)
viper.SetConfigType(DefaultConfigType)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()

// Default locations should be in the current directory or in a sub-directory called config.
// Future iterations should take a config file from an environmental variable via spf13/cobra
Expand Down Expand Up @@ -87,7 +90,6 @@ func ReadConfiguration() {
} else {
// Config file was found but another error was produced, so we will exit.
panic(fmt.Errorf("fatal error config file: %s", err))

}
}
}
3 changes: 2 additions & 1 deletion pkg/infrabin/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ func TestShutdown(t *testing.T) {
context.Background(),
"bufnet",
grpc.WithContextDialer(func(ctx context.Context, address string) (net.Conn, error) { return lis.Dial() }),
grpc.WithInsecure(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
t.Fatal(err) // could not set up test listener
Expand Down
8 changes: 4 additions & 4 deletions pkg/infrabin/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func TestAWSGetCallerIdentityHandler(t *testing.T) {
}

var rrJSON map[string]interface{}
if err := json.Unmarshal([]byte(rr.Body.String()), &rrJSON); err != nil {
if err := json.Unmarshal(rr.Body.Bytes(), &rrJSON); err != nil {
t.Fatalf("failed to parse responseRecorder body %v: %v", rr.Body.String(), err)
}

Expand Down Expand Up @@ -444,7 +444,7 @@ func TestIntermittentHandler(t *testing.T) {
}

var rrJSON map[string]interface{}
if err := json.Unmarshal([]byte(rr.Body.String()), &rrJSON); err != nil {
if err := json.Unmarshal(rr.Body.Bytes(), &rrJSON); err != nil {
t.Fatalf("failed to parse responseRecorder body %v: %v", rr.Body.String(), err)
}

Expand All @@ -464,7 +464,7 @@ func TestIntermittentHandler(t *testing.T) {
t.Fatalf("failed to parse responseString %v: %v", responseString, err)
}

if err := json.Unmarshal([]byte(rr.Body.String()), &rrJSON); err != nil {
if err := json.Unmarshal(rr.Body.Bytes(), &rrJSON); err != nil {
t.Fatalf("failed to parse responseRecorder body %v: %v", rr.Body.String(), err)
}

Expand All @@ -484,7 +484,7 @@ func TestIntermittentHandler(t *testing.T) {
t.Fatalf("failed to parse responseString %v: %v", responseString, err)
}

if err := json.Unmarshal([]byte(rr.Body.String()), &rrJSON); err != nil {
if err := json.Unmarshal(rr.Body.Bytes(), &rrJSON); err != nil {
t.Fatalf("failed to parse responseRecorder body %v: %v", rr.Body.String(), err)
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/infrabin/infrabin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ func (s *InfrabinService) Root(ctx context.Context, _ *Empty) (*Response, error)

var resp Response
resp.Hostname = hostname
// Take kubernetes info from a couple of _common_ environment variables
resp.Kubernetes = &KubeResponse{
PodName: helpers.GetEnv("POD_NAME", ""),
Namespace: helpers.GetEnv("POD_NAMESPACE", ""),
PodIp: helpers.GetEnv("POD_IP", ""),
NodeName: helpers.GetEnv("NODE_NAME", ""),
PodName: helpers.GetEnv("POD_NAME", "K8S_POD_NAME", ""),
Namespace: helpers.GetEnv("POD_NAMESPACE", "K8S_NAMESPACE", ""),
PodIp: helpers.GetEnv("POD_IP", "K8S_POD_IP", ""),
NodeName: helpers.GetEnv("NODE_NAME", "K8S_NODE_NAME", ""),
ClusterName: helpers.GetEnv("CLUSTER_NAME", "K8S_CLUSTER_NAME", ""),
Region: helpers.GetEnv("REGION", "AWS_REGION", "FUNCTION_REGION", ""),
}
return &resp, nil
}
Expand Down
2 changes: 2 additions & 0 deletions proto/infrabin/infrabin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ message KubeResponse {
string namespace = 2;
string pod_ip = 3;
string node_name = 4;
string cluster_name = 5;
string region = 6;
}

message GetCallerIdentityResponse {
Expand Down

0 comments on commit 08b78a9

Please sign in to comment.