Skip to content

Commit

Permalink
Add patch services
Browse files Browse the repository at this point in the history
  • Loading branch information
josedonizetti committed Feb 13, 2020
1 parent 649d5c0 commit 3f0fceb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
20 changes: 14 additions & 6 deletions pkg/minikube/tunnel/kic/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
typed_core "k8s.io/client-go/kubernetes/typed/core/v1"

"k8s.io/minikube/pkg/minikube/tunnel"
)

// Tunnel ...
type Tunnel struct {
sshPort string
sshKey string
v1Core typed_core.CoreV1Interface
sshPort string
sshKey string
v1Core typed_core.CoreV1Interface
LoadBalancerEmulator tunnel.LoadBalancerEmulator
}

// NewTunnel ...
func NewTunnel(sshPort, sshKey string, v1Core typed_core.CoreV1Interface) *Tunnel {
return &Tunnel{
sshPort: sshPort,
sshKey: sshKey,
v1Core: v1Core,
sshPort: sshPort,
sshKey: sshKey,
v1Core: v1Core,
LoadBalancerEmulator: tunnel.NewLoadBalancerEmulator(v1Core),
}
}

Expand Down Expand Up @@ -52,6 +56,10 @@ func (t *Tunnel) Start() error {

newSSHTunnel := createSSHTunnel(name, t.sshPort, t.sshKey, s)
sshTunnels[newSSHTunnel.name] = newSSHTunnel
_, err := t.LoadBalancerEmulator.PatchServicesIP("127.0.0.1")
if err != nil {
fmt.Println(err)
}
}
}

Expand Down
38 changes: 26 additions & 12 deletions pkg/minikube/tunnel/loadbalancer_patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,28 @@ type patchConverter interface {
convert(restClient rest.Interface, patch *Patch) *rest.Request
}

// loadBalancerEmulator is the main struct for emulating the loadbalancer behavior. it sets the ingress to the cluster IP
type loadBalancerEmulator struct {
// LoadBalancerEmulator is the main struct for emulating the loadbalancer behavior. it sets the ingress to the cluster IP
type LoadBalancerEmulator struct {
coreV1Client typed_core.CoreV1Interface
requestSender requestSender
patchConverter patchConverter
}

func (l *loadBalancerEmulator) PatchServices() ([]string, error) {
func (l *LoadBalancerEmulator) PatchServices() ([]string, error) {
return l.applyOnLBServices(l.updateService)
}

func (l *loadBalancerEmulator) Cleanup() ([]string, error) {
func (l *LoadBalancerEmulator) PatchServicesIP(ip string) ([]string, error) {
return l.applyOnLBServices(func(restClient rest.Interface, svc core.Service) ([]byte, error) {
return l.updateServiceIP(restClient, svc, ip)
})
}

func (l *LoadBalancerEmulator) Cleanup() ([]string, error) {
return l.applyOnLBServices(l.cleanupService)
}

func (l *loadBalancerEmulator) applyOnLBServices(action func(restClient rest.Interface, svc core.Service) ([]byte, error)) ([]string, error) {
func (l *LoadBalancerEmulator) applyOnLBServices(action func(restClient rest.Interface, svc core.Service) ([]byte, error)) ([]string, error) {
services := l.coreV1Client.Services("")
serviceList, err := services.List(meta.ListOptions{})
if err != nil {
Expand All @@ -79,14 +85,22 @@ func (l *loadBalancerEmulator) applyOnLBServices(action func(restClient rest.Int
}
return managedServices, nil
}
func (l *loadBalancerEmulator) updateService(restClient rest.Interface, svc core.Service) ([]byte, error) {

func (l *LoadBalancerEmulator) updateService(restClient rest.Interface, svc core.Service) ([]byte, error) {
clusterIP := svc.Spec.ClusterIP
ingresses := svc.Status.LoadBalancer.Ingress
if len(ingresses) == 1 && ingresses[0].IP == clusterIP {
return nil, nil
}
return l.updateServiceIP(restClient, svc, clusterIP)
}

func (l *LoadBalancerEmulator) updateServiceIP(restClient rest.Interface, svc core.Service, ip string) ([]byte, error) {
if len(ip) == 0 {
return nil, nil
}
glog.V(3).Infof("[%s] setting ClusterIP as the LoadBalancer Ingress", svc.Name)
jsonPatch := fmt.Sprintf(`[{"op": "add", "path": "/status/loadBalancer/ingress", "value": [ { "ip": "%s" } ] }]`, clusterIP)
jsonPatch := fmt.Sprintf(`[{"op": "add", "path": "/status/loadBalancer/ingress", "value": [ { "ip": "%s" } ] }]`, ip)
patch := &Patch{
Type: types.JSONPatchType,
ResourceName: svc.Name,
Expand All @@ -99,14 +113,14 @@ func (l *loadBalancerEmulator) updateService(restClient rest.Interface, svc core
request := l.patchConverter.convert(restClient, patch)
result, err := l.requestSender.send(request)
if err != nil {
glog.Errorf("error patching %s with IP %s: %s", svc.Name, clusterIP, err)
glog.Errorf("error patching %s with IP %s: %s", svc.Name, ip, err)
} else {
glog.Infof("Patched %s with IP %s", svc.Name, clusterIP)
glog.Infof("Patched %s with IP %s", svc.Name, ip)
}
return result, err
}

func (l *loadBalancerEmulator) cleanupService(restClient rest.Interface, svc core.Service) ([]byte, error) {
func (l *LoadBalancerEmulator) cleanupService(restClient rest.Interface, svc core.Service) ([]byte, error) {
ingresses := svc.Status.LoadBalancer.Ingress
if len(ingresses) == 0 {
return nil, nil
Expand All @@ -129,8 +143,8 @@ func (l *loadBalancerEmulator) cleanupService(restClient rest.Interface, svc cor

}

func newLoadBalancerEmulator(corev1Client typed_core.CoreV1Interface) loadBalancerEmulator {
return loadBalancerEmulator{
func NewLoadBalancerEmulator(corev1Client typed_core.CoreV1Interface) LoadBalancerEmulator {
return LoadBalancerEmulator{
coreV1Client: corev1Client,
requestSender: &defaultRequestSender{},
patchConverter: &defaultPatchConverter{},
Expand Down
8 changes: 4 additions & 4 deletions pkg/minikube/tunnel/loadbalancer_patcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestEmptyListOfServicesDoesNothing(t *testing.T) {
client := newStubCoreClient(&core.ServiceList{
Items: []core.Service{}})

patcher := newLoadBalancerEmulator(client)
patcher := NewLoadBalancerEmulator(client)

serviceNames, err := patcher.PatchServices()

Expand All @@ -113,7 +113,7 @@ func TestServicesWithNoLoadbalancerType(t *testing.T) {
},
})

patcher := newLoadBalancerEmulator(client)
patcher := NewLoadBalancerEmulator(client)

serviceNames, err := patcher.PatchServices()

Expand Down Expand Up @@ -214,7 +214,7 @@ func TestServicesWithLoadbalancerType(t *testing.T) {
requestSender := &countingRequestSender{}
patchConverter := &recordingPatchConverter{}

patcher := newLoadBalancerEmulator(client)
patcher := NewLoadBalancerEmulator(client)
patcher.requestSender = requestSender
patcher.patchConverter = patchConverter

Expand Down Expand Up @@ -328,7 +328,7 @@ func TestCleanupPatchedIPs(t *testing.T) {
requestSender := &countingRequestSender{}
patchConverter := &recordingPatchConverter{}

patcher := newLoadBalancerEmulator(client)
patcher := NewLoadBalancerEmulator(client)
patcher.requestSender = requestSender
patcher.patchConverter = patchConverter

Expand Down
8 changes: 4 additions & 4 deletions pkg/minikube/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func newTunnel(machineName string, machineAPI libmachine.API, configLoader confi
clusterInspector: ci,
router: router,
registry: registry,
loadBalancerEmulator: newLoadBalancerEmulator(v1Core),
LoadBalancerEmulator: NewLoadBalancerEmulator(v1Core),
status: &Status{
TunnelID: id,
MinikubeState: state,
Expand All @@ -88,7 +88,7 @@ type tunnel struct {
// collaborators
clusterInspector *clusterInspector
router router
loadBalancerEmulator loadBalancerEmulator
LoadBalancerEmulator LoadBalancerEmulator
reporter reporter
registry *persistentRegistry

Expand All @@ -108,7 +108,7 @@ func (t *tunnel) cleanup() *Status {
}
}
if t.status.MinikubeState == Running {
t.status.PatchedServices, t.status.LoadBalancerEmulatorError = t.loadBalancerEmulator.Cleanup()
t.status.PatchedServices, t.status.LoadBalancerEmulatorError = t.LoadBalancerEmulator.Cleanup()
}
return t.status
}
Expand All @@ -122,7 +122,7 @@ func (t *tunnel) update() *Status {
glog.V(3).Infof("minikube is running, trying to add route%s", t.status.TunnelID.Route)
setupRoute(t, h)
if t.status.RouteError == nil {
t.status.PatchedServices, t.status.LoadBalancerEmulatorError = t.loadBalancerEmulator.PatchServices()
t.status.PatchedServices, t.status.LoadBalancerEmulatorError = t.LoadBalancerEmulator.PatchServices()
}
}
glog.V(3).Infof("sending report %s", t.status)
Expand Down

0 comments on commit 3f0fceb

Please sign in to comment.