Skip to content

Improve logging #9

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 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ SC_ACCESS_TOKEN=jwt_token
SC_LOCATION_ID=1

# if missing or empty prod api url will be used
SC_API_URL=https://api.dev1.privateservergrid.com/v1
SC_API_URL=
6 changes: 5 additions & 1 deletion internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
k8sopts "k8s.io/component-base/config/options"
)

const (
DefaultScIngressClass = "serverscom"
)

// ParseFlags parses os args and map them to controller configuration
func ParseFlags() (*controller.Configuration, error) {
var (
Expand All @@ -24,7 +28,7 @@ func ParseFlags() (*controller.Configuration, error) {
watchNamespace = flags.String("watch-namespace", v1.NamespaceAll,
`Namespace to watch for Ingress/Services/Endpoints.`)

ingressClass = flags.String("ingress-class", "",
ingressClass = flags.String("ingress-class", DefaultScIngressClass,
`If set, overrides what ingress classes are managed by the controller.`)

resyncPeriod = flags.Duration("sync-period", 0,
Expand Down
2 changes: 1 addition & 1 deletion internal/ingress/controller/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *Store) GetNodesIpList() []string {
return s.listers.Node.NodesIpList()
}

// GetSecret returns the Secret matching key.
// GetIngressServiceInfo returns ingress services info.
func (s *Store) GetIngressServiceInfo(ingress *networkv1.Ingress) (map[string]ServiceInfo, error) {
return getIngressServiceInfo(ingress, s)
}
Expand Down
11 changes: 10 additions & 1 deletion internal/ingress/controller/store/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store

import (
"errors"
"testing"
"time"

Expand All @@ -11,7 +12,7 @@ import (
)

var (
scIngressClassName = "sc-ingress"
scIngressClassName = "serverscom"
nonScIngressClassName = "not-sc-ingress"
scIngress = &networkv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -200,4 +201,12 @@ func TestGetIngressServiceInfo(t *testing.T) {
g.Expect(serviceInfo[serviceName].NodePort).To(Equal(30000))
g.Expect(serviceInfo[serviceName].NodeIps).To(ConsistOf("192.168.1.1"))
g.Expect(serviceInfo[serviceName].Annotations).To(HaveKeyWithValue("key", "value"))

// check if service doens't have NodePort
service.Spec.Ports[0].NodePort = 0
s.listers.Service.Update(service)

serviceInfo, err = s.GetIngressServiceInfo(ingress)
expectedErr := errors.New("service doesn't have NodePort, only services with type 'NodePort' or 'LoadBalancer' supported")
g.Expect(err).To(Equal(expectedErr))
}
2 changes: 2 additions & 0 deletions internal/ingress/controller/store/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func getIngressServiceInfo(ingress *networkv1.Ingress, store Storer) (map[string
sTmp.Hosts = append(sTmp.Hosts, rule.Host)
servicesInfo[serviceName] = sTmp
}
} else {
return nil, fmt.Errorf("service doesn't have NodePort, only services with type 'NodePort' or 'LoadBalancer' supported")
}
break
}
Expand Down
8 changes: 2 additions & 6 deletions internal/ingress/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ package ingress
import v1 "k8s.io/api/networking/v1"

const (
IngressClassKey = "kubernetes.io/ingress.class"
DefaultScIngressClass = "serverscom"
IngressClassKey = "kubernetes.io/ingress.class"
)

// IsScIngress checks if Ingress belongs to a specified class or DefaultScIngressClass if class empty
// IsScIngress checks if Ingress belongs to a specified class
func IsScIngress(i *v1.Ingress, class string) bool {
if class == "" {
class = DefaultScIngressClass
}
if i.Spec.IngressClassName != nil {
return *i.Spec.IngressClassName == class
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ingress/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestIsScIngress(t *testing.T) {
g := NewWithT(t)

defaultClass := DefaultScIngressClass
defaultClass := "serverscom"
otherClass := "other-class"

g.Expect(IsScIngress(&v1.Ingress{}, "")).To(BeFalse())
Expand Down
5 changes: 5 additions & 0 deletions internal/service/loadbalancer/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package loadbalancer

import (
"errors"
"fmt"
"strconv"
"sync"
Expand Down Expand Up @@ -197,6 +198,10 @@ func (m *Manager) TranslateIngressToLB(ingress *networkv1.Ingress, sslCerts map[
uZInput = *annotations.FillLBUpstreamZoneWithServiceAnnotations(&uZInput, service.Annotations)
upstreamZones = append(upstreamZones, uZInput)
}
if len(vhostZones) == 0 || len(upstreamZones) == 0 {
return nil, errors.New("vhost or upstream can't be empty, can't continue")
}

locIdStr := config.FetchEnv("SC_LOCATION_ID", "1")
locId, err := strconv.Atoi(locIdStr)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions internal/service/loadbalancer/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,9 @@ func TestTranslateIngressToLB(t *testing.T) {
g := NewWithT(t)
storeHandler.EXPECT().GetIngressServiceInfo(ingress).Return(make(map[string]store.ServiceInfo), nil)
lbInput, err := manager.TranslateIngressToLB(ingress, sslCerts)
g.Expect(err).To(BeNil())
g.Expect(lbInput).NotTo(BeNil())
g.Expect(lbInput.UpstreamZones).To(BeEmpty())
g.Expect(lbInput.VHostZones).To(BeEmpty())
expectedErr := errors.New("vhost or upstream can't be empty, can't continue")
g.Expect(err).To(Equal(expectedErr))
g.Expect(lbInput).To(BeNil())
})
}

Expand Down
4 changes: 3 additions & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ func (s *Service) SyncToPortal(key string) error {
if err != nil {
s.recorder.Eventf(ing, v1.EventTypeWarning, "UpdateStatus", err.Error())
}

s.recorder.Eventf(ing, v1.EventTypeNormal, "Synced", "Successfully synced")
}()

s.recorder.Eventf(ing, v1.EventTypeNormal, "Synced", "Successfully synced to portal")
s.recorder.Eventf(ing, v1.EventTypeNormal, "Created", "Successfully created")

return nil
}
4 changes: 2 additions & 2 deletions internal/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestSyncToPortal(t *testing.T) {
}
}
g.Expect(events).To(ContainElements(
"Normal Synced Successfully synced to portal",
"Normal Synced Successfully synced",
`Warning UpdateStatus ingresses.networking.k8s.io "test-ingress" not found`,
))
})
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestSyncToPortal(t *testing.T) {

select {
case e := <-recorder.Events:
expectedEvent := "Normal Synced Successfully synced to portal"
expectedEvent := "Normal Created Successfully created"
g.Expect(e).To(BeEquivalentTo(expectedEvent))
case <-time.After(time.Second * 1):
t.Fatal("Timeout waiting for event")
Expand Down
Loading