fix linter issues (#329)

* fix linter issues
* replace fmt with logger
Signed-off-by: Fabian Kammel <fk@edgeless.systems>
Co-authored-by: Daniel Weiße <66256922+daniel-weisse@users.noreply.github.com>
This commit is contained in:
Fabian Kammel 2022-08-02 16:25:47 +02:00 committed by GitHub
parent 1859dc1718
commit 985585f578
8 changed files with 41 additions and 16 deletions

View File

@ -1,6 +1,7 @@
package qemu
import (
"context"
"net/http"
"net/url"
"strings"
@ -23,7 +24,12 @@ func (l *Logger) Disclose(msg string) {
Path: "/log",
}
_, _ = http.Post(url.String(), "application/json", strings.NewReader(msg))
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, url.String(), strings.NewReader(msg))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err == nil {
defer resp.Body.Close()
}
}
// Close is a no-op.

View File

@ -259,13 +259,18 @@ func (k *KubernetesUtil) setupGCPPodNetwork(ctx context.Context, nodeName, nodeP
// FixCilium fixes https://github.com/cilium/cilium/issues/19958 but instead of a rollout restart of
// the cilium daemonset, it only restarts the local cilium pod.
func (k *KubernetesUtil) FixCilium(nodeNameK8s string) {
func (k *KubernetesUtil) FixCilium(nodeNameK8s string, log *logger.Logger) {
// wait for cilium pod to be healthy
for {
time.Sleep(5 * time.Second)
resp, err := http.Get("http://127.0.0.1:9876/healthz")
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://127.0.0.1:9876/healthz", nil)
if err != nil {
fmt.Printf("waiting for local cilium daemonset pod not healthy: %v\n", err)
log.With(zap.Error(err)).Errorf("Unable to create request")
continue
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.With(zap.Error(err)).Warnf("Waiting for local cilium daemonset pod not healthy")
continue
}
resp.Body.Close()
@ -277,22 +282,21 @@ func (k *KubernetesUtil) FixCilium(nodeNameK8s string) {
// get cilium pod name
out, err := exec.CommandContext(context.Background(), "/bin/bash", "-c", "/run/state/bin/crictl ps -o json | jq -r '.containers[] | select(.metadata.name == \"cilium-agent\") | .podSandboxId'").CombinedOutput()
if err != nil {
fmt.Printf("getting pod id failed: %v: %v\n", err, string(out))
log.With(zap.Error(err)).Errorf("Getting pod id failed: %s", out)
return
}
outLines := strings.Split(string(out), "\n")
fmt.Println(outLines)
podID := outLines[len(outLines)-2]
// stop and delete pod
out, err = exec.CommandContext(context.Background(), "/run/state/bin/crictl", "stopp", podID).CombinedOutput()
if err != nil {
fmt.Printf("stopping cilium agent pod failed: %v: %v\n", err, string(out))
log.With(zap.Error(err)).Errorf("Stopping cilium agent pod failed: %s", out)
return
}
out, err = exec.CommandContext(context.Background(), "/run/state/bin/crictl", "rmp", podID).CombinedOutput()
if err != nil {
fmt.Printf("removing cilium agent pod failed: %v: %v\n", err, string(out))
log.With(zap.Error(err)).Errorf("Removing cilium agent pod failed: %s", out)
}
}

View File

@ -25,5 +25,5 @@ type clusterUtil interface {
SetupGCPGuestAgent(kubectl k8sapi.Client, gcpGuestAgentConfiguration resources.Marshaler) error
StartKubelet() error
RestartKubelet() error
FixCilium(nodeNameK8s string)
FixCilium(nodeNameK8s string, log *logger.Logger)
}

View File

@ -222,7 +222,7 @@ func (k *KubeWrapper) InitCluster(
return nil, fmt.Errorf("failed to setup k8s version ConfigMap: %v", err)
}
k.clusterUtil.FixCilium(nodeName)
k.clusterUtil.FixCilium(nodeName, log)
return k.GetKubeconfig()
}
@ -286,7 +286,7 @@ func (k *KubeWrapper) JoinCluster(ctx context.Context, args *kubeadm.BootstrapTo
return fmt.Errorf("joining cluster: %v; %w ", string(joinConfigYAML), err)
}
k.clusterUtil.FixCilium(nodeName)
k.clusterUtil.FixCilium(nodeName, log)
return nil
}

View File

@ -579,7 +579,7 @@ func (s *stubClusterUtil) RestartKubelet() error {
return s.restartKubeletErr
}
func (s *stubClusterUtil) FixCilium(nodeName string) {
func (s *stubClusterUtil) FixCilium(nodeName string, log *logger.Logger) {
}
type stubConfigProvider struct {

View File

@ -146,7 +146,10 @@ func TestUpdate(t *testing.T) {
require.NoError(validator.Update())
// client connection should fail now, since the server's validator expects a different OID from the client
_, err = testConnection(require, server.URL, clientOID)
resp, err = testConnection(require, server.URL, clientOID)
if err == nil {
defer resp.Body.Close()
}
assert.Error(err)
}

View File

@ -134,6 +134,16 @@ func exportPCRs() error {
Host: "10.42.0.1:8080", // QEMU metadata endpoint
Path: "/pcrs",
}
_, err = http.Post(url.String(), "application/json", bytes.NewBuffer(pcrsPretty))
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url.String(), bytes.NewBuffer(pcrsPretty))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}

View File

@ -211,7 +211,9 @@ func TestGetAttestationHTTP(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(server.getAttestationHTTP))
defer httpServer.Close()
resp, err := http.Get(httpServer.URL + tc.request)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, httpServer.URL+tc.request, nil)
require.NoError(err)
resp, err := http.DefaultClient.Do(req)
require.NoError(err)
defer resp.Body.Close()