Skip to content

Commit

Permalink
small misc cleanups
Browse files Browse the repository at this point in the history
* gitignore fixup
* add license header to files
* import whole auth package, not just azure and gcp
* examples for tap interface. These will probably change in the
  future but it's a start
* mitmproxy tests broken out, function renames
Matt Hamilton committed May 30, 2020

Unverified

No user is associated with the committer email.
1 parent 6a47445 commit c29e47a
Showing 6 changed files with 79 additions and 41 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -15,10 +15,8 @@ ci.mod
ci.sum
ig-tests.mod
ig-tests.sum
docs/_book/
*_original
site/
.vim/
venv/
coverage.*
krew/
29 changes: 21 additions & 8 deletions cmd/kubectl-tap/mitmproxy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2020 Soluble Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main

import (
@@ -141,14 +154,14 @@ func (m *Mitmproxy) String() string {
func (m *Mitmproxy) ReadyEnv() error {
configmapsClient := m.Client.CoreV1().ConfigMaps(m.ProxyOpts.Namespace)
// Create the ConfigMap based the options we're configuring mitmproxy with
if err := createConfigMap(configmapsClient, m.ProxyOpts); err != nil {
if err := createMitmproxyConfigMap(configmapsClient, m.ProxyOpts); err != nil {
// If the service hasn't been tapped but still has a configmap from a previous
// run (which can happen if the deployment borks and "tap off" isn't explicitly run,
// delete the configmap and try again.
// This is mostly here to fix development environments that become broken during
// code testing.
_ = destroyConfigMap(configmapsClient, m.ProxyOpts.Target)
rErr := createConfigMap(configmapsClient, m.ProxyOpts)
_ = destroyMitmproxyConfigMap(configmapsClient, m.ProxyOpts.Target)
rErr := createMitmproxyConfigMap(configmapsClient, m.ProxyOpts)
if rErr != nil {
if errors.Is(os.ErrInvalid, rErr) {
return fmt.Errorf("there was an unexpected problem creating the ConfigMap")
@@ -162,12 +175,12 @@ func (m *Mitmproxy) ReadyEnv() error {
// UnreadyEnv removes tap supporting configmap.
func (m *Mitmproxy) UnreadyEnv() error {
configmapsClient := m.Client.CoreV1().ConfigMaps(m.ProxyOpts.Namespace)
return destroyConfigMap(configmapsClient, m.ProxyOpts.Target)
return destroyMitmproxyConfigMap(configmapsClient, m.ProxyOpts.Target)
}

// createConfigMap creates a mitmproxy configmap based on the proxy mode, however currently
// createMitmproxyConfigMap creates a mitmproxy configmap based on the proxy mode, however currently
// only "reverse" mode is supported.
func createConfigMap(configmapClient corev1.ConfigMapInterface, proxyOpts ProxyOptions) error {
func createMitmproxyConfigMap(configmapClient corev1.ConfigMapInterface, proxyOpts ProxyOptions) error {
// TODO: eventually, we should build a struct and use yaml to marshal this,
// but for now we're just doing string concatenation.
var mitmproxyConfig []byte
@@ -223,8 +236,8 @@ func createConfigMap(configmapClient corev1.ConfigMapInterface, proxyOpts ProxyO
return nil
}

// destroyConfigMap removes a mitmproxy ConfigMap from the environment.
func destroyConfigMap(configmapClient corev1.ConfigMapInterface, serviceName string) error {
// destroyMitmproxyConfigMap removes a mitmproxy ConfigMap from the environment.
func destroyMitmproxyConfigMap(configmapClient corev1.ConfigMapInterface, serviceName string) error {
if serviceName == "" {
return os.ErrInvalid
}
51 changes: 51 additions & 0 deletions cmd/kubectl-tap/mitmproxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020 Soluble Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main

import (
"errors"
"os"
"testing"

"github.com/stretchr/testify/require"
"k8s.io/client-go/kubernetes/fake"
)

func Test_DestroyMitmproxyConfigMap(t *testing.T) {
tests := []struct {
Name string
ServiceName string
ClientFunc func() *fake.Clientset
Err error
}{
{"simple", "sample-service", fakeClientTappedSimple, nil},
{"untapped", "sample-service", fakeClientUntappedSimple, ErrConfigMapNoMatch},
{"no_svc_name", "", fakeClientTappedSimple, os.ErrInvalid},
{"missing_annotations", "sample-service", fakeClientTappedWithoutAnnotations, ErrConfigMapNoMatch},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
require := require.New(t)
fakeClient := tc.ClientFunc()
cmClient := fakeClient.CoreV1().ConfigMaps("default")
err := destroyMitmproxyConfigMap(cmClient, tc.ServiceName)
if tc.Err != nil {
require.NotNil(err)
require.True(errors.Is(err, tc.Err))
} else {
require.Nil(err)
}
})
}
}
8 changes: 6 additions & 2 deletions cmd/kubectl-tap/tap.go
Original file line number Diff line number Diff line change
@@ -43,8 +43,7 @@ import (
"k8s.io/client-go/transport/spdy"
"k8s.io/client-go/util/retry"

_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)

const (
@@ -91,8 +90,13 @@ type Tap interface {

// PatchDeployment tweaks a Deployment after a Sidecar is added
// during the tap process.
// Example: mitmproxy calls this function to configure the ConfigMap volume refs.
PatchDeployment(*k8sappsv1.Deployment)

// ReadyEnv and UnreadyEnv are used to prepare the environment
// with resources that will be necessary for the sidecar, but do
// not exist within a given Deployment.
// Example: mitmproxy calls this function to apply and remove ConfigMaps for mitmproxy.
ReadyEnv() error
UnreadyEnv() error

29 changes: 0 additions & 29 deletions cmd/kubectl-tap/tap_test.go
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@ import (
"context"
"errors"
"io/ioutil"
"os"
"strings"
"testing"

@@ -180,34 +179,6 @@ func Test_NewListCommand(t *testing.T) {
}
}

func Test_DestroyConfigMap(t *testing.T) {
tests := []struct {
Name string
ServiceName string
ClientFunc func() *fake.Clientset
Err error
}{
{"simple", "sample-service", fakeClientTappedSimple, nil},
{"untapped", "sample-service", fakeClientUntappedSimple, ErrConfigMapNoMatch},
{"no_svc_name", "", fakeClientTappedSimple, os.ErrInvalid},
{"missing_annotations", "sample-service", fakeClientTappedWithoutAnnotations, ErrConfigMapNoMatch},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
require := require.New(t)
fakeClient := tc.ClientFunc()
cmClient := fakeClient.CoreV1().ConfigMaps("default")
err := destroyConfigMap(cmClient, tc.ServiceName)
if tc.Err != nil {
require.NotNil(err)
require.True(errors.Is(err, tc.Err))
} else {
require.Nil(err)
}
})
}
}

var (
simpleNamespace = v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -134,6 +134,7 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
github.com/googleapis/gnostic v0.1.0 h1:rVsPeBmXbYv4If/cumu1AzZPwV58q433hvONV1UEZoI=
github.com/googleapis/gnostic v0.1.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
github.com/gophercloud/gophercloud v0.1.0 h1:P/nh25+rzXouhytV2pUHBb65fnds26Ghl8/391+sT5o=
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=

0 comments on commit c29e47a

Please sign in to comment.