Don't add loadbalancer IP routing rule on workers

This commit is contained in:
katexochen 2022-08-22 11:44:26 +02:00 committed by Paul Meyer
parent 2b25862c33
commit e57c3991f7
7 changed files with 82 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi" "github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi"
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/kubectl" "github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/kubectl"
"github.com/edgelesssys/constellation/bootstrapper/internal/logging" "github.com/edgelesssys/constellation/bootstrapper/internal/logging"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/atls" "github.com/edgelesssys/constellation/internal/atls"
"github.com/edgelesssys/constellation/internal/attestation/azure" "github.com/edgelesssys/constellation/internal/attestation/azure"
"github.com/edgelesssys/constellation/internal/attestation/gcp" "github.com/edgelesssys/constellation/internal/attestation/gcp"
@ -168,6 +169,13 @@ func main() {
} }
func setLoadbalancerRoute(ctx context.Context, meta metadataAPI) error { func setLoadbalancerRoute(ctx context.Context, meta metadataAPI) error {
self, err := meta.Self(ctx)
if err != nil {
return err
}
if self.Role != role.ControlPlane {
return nil
}
endpoint, err := meta.GetLoadBalancerEndpoint(ctx) endpoint, err := meta.GetLoadBalancerEndpoint(ctx)
if err != nil { if err != nil {
return err return err

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"sync" "sync"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/debugd/bootstrapper" "github.com/edgelesssys/constellation/debugd/bootstrapper"
"github.com/edgelesssys/constellation/debugd/debugd/deploy" "github.com/edgelesssys/constellation/debugd/debugd/deploy"
"github.com/edgelesssys/constellation/debugd/debugd/metadata" "github.com/edgelesssys/constellation/debugd/debugd/metadata"
@ -99,6 +100,13 @@ func writeDebugBanner(log *logger.Logger) {
} }
func setLoadbalancerRoute(ctx context.Context, fetcher metadata.Fetcher) error { func setLoadbalancerRoute(ctx context.Context, fetcher metadata.Fetcher) error {
ownRole, err := fetcher.Role(ctx)
if err != nil {
return err
}
if ownRole != role.ControlPlane {
return nil
}
ip, err := fetcher.DiscoverLoadbalancerIP(ctx) ip, err := fetcher.DiscoverLoadbalancerIP(ctx)
if err != nil { if err != nil {
return err return err

View File

@ -8,6 +8,7 @@ import (
azurecloud "github.com/edgelesssys/constellation/bootstrapper/cloudprovider/azure" azurecloud "github.com/edgelesssys/constellation/bootstrapper/cloudprovider/azure"
gcpcloud "github.com/edgelesssys/constellation/bootstrapper/cloudprovider/gcp" gcpcloud "github.com/edgelesssys/constellation/bootstrapper/cloudprovider/gcp"
qemucloud "github.com/edgelesssys/constellation/bootstrapper/cloudprovider/qemu" qemucloud "github.com/edgelesssys/constellation/bootstrapper/cloudprovider/qemu"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/cloud/metadata" "github.com/edgelesssys/constellation/internal/cloud/metadata"
"github.com/edgelesssys/constellation/internal/deploy/ssh" "github.com/edgelesssys/constellation/internal/deploy/ssh"
) )
@ -57,6 +58,15 @@ func NewQEMU() *Fetcher {
} }
} }
func (f *Fetcher) Role(ctx context.Context) (role.Role, error) {
self, err := f.metaAPI.Self(ctx)
if err != nil {
return role.Unknown, fmt.Errorf("retrieving role from cloud provider metadata: %w", err)
}
return self.Role, nil
}
// DiscoverDebugdIPs will query the metadata of all instances and return any ips of instances already set up for debugging. // DiscoverDebugdIPs will query the metadata of all instances and return any ips of instances already set up for debugging.
func (f *Fetcher) DiscoverDebugdIPs(ctx context.Context) ([]string, error) { func (f *Fetcher) DiscoverDebugdIPs(ctx context.Context) ([]string, error) {
self, err := f.metaAPI.Self(ctx) self, err := f.metaAPI.Self(ctx)

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"testing" "testing"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/cloud/metadata" "github.com/edgelesssys/constellation/internal/cloud/metadata"
"github.com/edgelesssys/constellation/internal/deploy/ssh" "github.com/edgelesssys/constellation/internal/deploy/ssh"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -19,6 +20,48 @@ func TestMain(m *testing.M) {
) )
} }
func TestRole(t *testing.T) {
instance1 := metadata.InstanceMetadata{Role: role.ControlPlane}
instance2 := metadata.InstanceMetadata{Role: role.Worker}
testCases := map[string]struct {
meta *stubMetadata
wantErr bool
wantRole role.Role
}{
"control plane": {
meta: &stubMetadata{selfRes: instance1},
wantRole: role.ControlPlane,
},
"worker": {
meta: &stubMetadata{selfRes: instance2},
wantRole: role.Worker,
},
"self fails": {
meta: &stubMetadata{selfErr: errors.New("some err")},
wantErr: true,
wantRole: role.Unknown,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
fetcher := Fetcher{tc.meta}
role, err := fetcher.Role(context.Background())
if tc.wantErr {
assert.Error(err)
} else {
assert.NoError(err)
assert.Equal(tc.wantRole, role)
}
})
}
}
func TestDiscoverDebugIPs(t *testing.T) { func TestDiscoverDebugIPs(t *testing.T) {
err := errors.New("some err") err := errors.New("some err")

View File

@ -3,12 +3,18 @@ package fallback
import ( import (
"context" "context"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/deploy/ssh" "github.com/edgelesssys/constellation/internal/deploy/ssh"
) )
// Fetcher implements metadata.Fetcher interface but does not actually fetch cloud provider metadata. // Fetcher implements metadata.Fetcher interface but does not actually fetch cloud provider metadata.
type Fetcher struct{} type Fetcher struct{}
func (f Fetcher) Role(_ context.Context) (role.Role, error) {
// Fallback fetcher does not try to fetch role
return role.Unknown, nil
}
func (f Fetcher) DiscoverDebugdIPs(ctx context.Context) ([]string, error) { func (f Fetcher) DiscoverDebugdIPs(ctx context.Context) ([]string, error) {
// Fallback fetcher does not try to discover debugd IPs // Fallback fetcher does not try to discover debugd IPs
return nil, nil return nil, nil

View File

@ -7,6 +7,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/debugd/debugd" "github.com/edgelesssys/constellation/debugd/debugd"
"github.com/edgelesssys/constellation/internal/deploy/ssh" "github.com/edgelesssys/constellation/internal/deploy/ssh"
"github.com/edgelesssys/constellation/internal/logger" "github.com/edgelesssys/constellation/internal/logger"
@ -15,6 +16,7 @@ import (
// Fetcher retrieves other debugd IPs and SSH keys from cloud provider metadata. // Fetcher retrieves other debugd IPs and SSH keys from cloud provider metadata.
type Fetcher interface { type Fetcher interface {
Role(ctx context.Context) (role.Role, error)
DiscoverDebugdIPs(ctx context.Context) ([]string, error) DiscoverDebugdIPs(ctx context.Context) ([]string, error)
FetchSSHKeys(ctx context.Context) ([]ssh.UserKey, error) FetchSSHKeys(ctx context.Context) ([]ssh.UserKey, error)
DiscoverLoadbalancerIP(ctx context.Context) (string, error) DiscoverLoadbalancerIP(ctx context.Context) (string, error)

View File

@ -7,6 +7,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/edgelesssys/constellation/bootstrapper/role"
"github.com/edgelesssys/constellation/internal/deploy/ssh" "github.com/edgelesssys/constellation/internal/deploy/ssh"
"github.com/edgelesssys/constellation/internal/logger" "github.com/edgelesssys/constellation/internal/logger"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -91,6 +92,10 @@ type stubFetcher struct {
fetchSSHKeysErr error fetchSSHKeysErr error
} }
func (s *stubFetcher) Role(_ context.Context) (role.Role, error) {
return role.Unknown, nil
}
func (s *stubFetcher) DiscoverDebugdIPs(ctx context.Context) ([]string, error) { func (s *stubFetcher) DiscoverDebugdIPs(ctx context.Context) ([]string, error) {
s.discoverCalls++ s.discoverCalls++
return s.ips, s.discoverErr return s.ips, s.discoverErr