-
Notifications
You must be signed in to change notification settings - Fork 118
Support service resolution #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: coffee | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: coffee | ||
template: | ||
metadata: | ||
labels: | ||
app: coffee | ||
spec: | ||
containers: | ||
- name: coffee | ||
image: nginxdemos/nginx-hello:plain-text | ||
ports: | ||
- containerPort: 8080 | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: coffee | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: coffee | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: tea | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: tea | ||
template: | ||
metadata: | ||
labels: | ||
app: tea | ||
spec: | ||
containers: | ||
- name: tea | ||
image: nginxdemos/nginx-hello:plain-text | ||
ports: | ||
- containerPort: 8080 | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: tea | ||
labels: | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: tea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
"github.com/nginxinc/nginx-gateway-kubernetes/internal/config" | ||
"github.com/nginxinc/nginx-gateway-kubernetes/internal/events" | ||
"github.com/nginxinc/nginx-gateway-kubernetes/pkg/sdk" | ||
apiv1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
type serviceImplementation struct { | ||
conf config.Config | ||
eventCh chan<- interface{} | ||
} | ||
|
||
// TO-DO: serviceImplementation looks similar to httpRouteImplemenation | ||
// consider if it is possible to reduce the amount of code. | ||
|
||
// NewServiceImplementation creates a new ServiceImplementation. | ||
func NewServiceImplementation(cfg config.Config, eventCh chan<- interface{}) sdk.ServiceImpl { | ||
return &serviceImplementation{ | ||
conf: cfg, | ||
eventCh: eventCh, | ||
} | ||
} | ||
|
||
func (impl *serviceImplementation) Logger() logr.Logger { | ||
return impl.conf.Logger | ||
} | ||
|
||
func (impl *serviceImplementation) Upsert(svc *apiv1.Service) { | ||
impl.Logger().Info("Service was upserted", | ||
"namespace", svc.Namespace, "name", svc.Name, | ||
) | ||
|
||
impl.eventCh <- &events.UpsertEvent{ | ||
Resource: svc, | ||
} | ||
} | ||
|
||
func (impl *serviceImplementation) Remove(nsname types.NamespacedName) { | ||
impl.Logger().Info("Service resource was removed", | ||
"namespace", nsname.Namespace, "name", nsname.Name, | ||
) | ||
|
||
impl.eventCh <- &events.DeleteEvent{ | ||
NamespacedName: nsname, | ||
Type: &apiv1.Service{}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ServiceStore | ||
|
||
// ServiceStore stores services and can be queried for the cluster IP of a service. | ||
type ServiceStore interface { | ||
// Upsert upserts the service into the store. | ||
Upsert(svc *v1.Service) | ||
// Delete deletes the service from the store. | ||
Delete(nsname types.NamespacedName) | ||
// Resolve returns the cluster IP the service specified by its namespace and name. | ||
// If the service doesn't have a cluster IP or it doesn't exist, resolve will return an error. | ||
// TO-DO: later, we will start using the Endpoints rather than cluster IPs. | ||
Resolve(nsname types.NamespacedName) (string, error) | ||
} | ||
|
||
// NewServiceStore creates a new ServiceStore. | ||
func NewServiceStore() ServiceStore { | ||
f5yacobucci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &serviceStoreImpl{ | ||
services: make(map[string]*v1.Service), | ||
} | ||
} | ||
|
||
type serviceStoreImpl struct { | ||
services map[string]*v1.Service | ||
f5yacobucci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (s *serviceStoreImpl) Upsert(svc *v1.Service) { | ||
s.services[getResourceKey(&svc.ObjectMeta)] = svc | ||
} | ||
|
||
func (s *serviceStoreImpl) Delete(nsname types.NamespacedName) { | ||
delete(s.services, nsname.String()) | ||
} | ||
|
||
func (s *serviceStoreImpl) Resolve(nsname types.NamespacedName) (string, error) { | ||
svc, exist := s.services[nsname.String()] | ||
if !exist { | ||
return "", fmt.Errorf("service %s doesn't exist", nsname.String()) | ||
} | ||
|
||
if svc.Spec.ClusterIP == "" || svc.Spec.ClusterIP == "None" { | ||
return "", fmt.Errorf("service %s doesn't have ClusterIP", nsname.String()) | ||
} | ||
|
||
return svc.Spec.ClusterIP, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.