Skip to content
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

WIP:Add architecture and os checks when fetching tags #60

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions deploy/charts/version-checker/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ rules:
- ""
resources:
- "pods"
- "nodes"
verbs:
- "get"
- "list"
Expand Down
2 changes: 1 addition & 1 deletion deploy/yaml/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ metadata:
name: version-checker
rules:
- apiGroups: [""]
resources: ["pods"]
resources: ["pods","nodes"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to update deploy/yaml/deploy.yaml

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean the Helm chart?

Isn't the deploy/yaml/deploy.yaml changed already?

verbs: ["get", "watch", "list"]
---
kind: ClusterRoleBinding
Expand Down
17 changes: 12 additions & 5 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,20 @@ type Options struct {
PinPatch *int64 `json:"pin-patch,omitempty"`

RegexMatcher *regexp.Regexp `json:"-"`

// Architecture and OS to search for
Architecture *Architecture `json:"pin-architecture,omitempty"`
OS *OS `json:"pin-os,omitempty"`
}

// ImageTag describes a container image tag.
type ImageTag struct {
Tag string `json:"tag"`
SHA string `json:"sha"`
Timestamp time.Time `json:"timestamp"`
Architecture string `json:"architecture,omitempty"`
OS string `json:"os,omitempty"`
Tag string `json:"tag"`
SHA string `json:"sha"`
Timestamp time.Time `json:"timestamp"`
Architecture Architecture `json:"architecture,omitempty"`
OS OS `json:"os,omitempty"`
}

type Architecture string
type OS string
67 changes: 67 additions & 0 deletions pkg/checker/architecture/architecture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package architecture

import (
"errors"
"fmt"
"sync"

corev1 "k8s.io/api/core/v1"

"github.com/jetstack/version-checker/pkg/api"
)

// NodeMetadata metadata about a particular node
type nodeMetadata struct {
OS api.OS
Architecture api.Architecture
}

type NodeMap struct {
mu sync.RWMutex
nodes map[string]*nodeMetadata
}

func New() *NodeMap {
// might need to pass an initial map
return &NodeMap{
nodes: make(map[string]*nodeMetadata),
}
}

func (m *NodeMap) GetArchitecture(nodeName string) (*nodeMetadata, bool) {
m.mu.RLock()
defer m.mu.RUnlock()

meta, ok := m.nodes[nodeName]
return meta, ok
}

func (m *NodeMap) Add(node *corev1.Node) error {
m.mu.Lock()
defer m.mu.Unlock()
if node == nil {
return errors.New("passed node is nil")
}

arch, ok := node.Labels[corev1.LabelArchStable]
if !ok {
return fmt.Errorf("missing %q label on node %q", corev1.LabelArchStable, node.Name)
}

os, ok := node.Labels[corev1.LabelOSStable]
if !ok {
return fmt.Errorf("missing %q label on node %q", corev1.LabelOSStable, node.Name)
}

m.nodes[node.Name] = &nodeMetadata{
OS: api.OS(os),
Architecture: api.Architecture(arch),
}
return nil
}

func (m *NodeMap) Delete(nodeName string) {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.nodes, nodeName)
}
156 changes: 156 additions & 0 deletions pkg/checker/architecture/architecture_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package architecture

import (
"errors"
"fmt"
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

utils "github.com/jetstack/version-checker/pkg/checker/internal/test"
)

func TestAdd(t *testing.T) {
tests := map[string]struct {
input []*corev1.Node
expOperationOutput error
expResult map[string]*nodeMetadata
}{
"update valid node": {
input: []*corev1.Node{
utils.CreateNode("node1", utils.ArchAMD64, utils.OSLinux),
utils.CreateNode("node1", utils.ArchARM, utils.OSLinux),
},
expOperationOutput: nil,
expResult: map[string]*nodeMetadata{
"node1": &nodeMetadata{
Architecture: utils.ArchARM,
OS: utils.OSLinux,
},
},
},
"add valid node": {
input: []*corev1.Node{
utils.CreateNode("node1", utils.ArchAMD64, utils.OSLinux),
},
expOperationOutput: nil,
expResult: map[string]*nodeMetadata{
"node1": &nodeMetadata{
Architecture: utils.ArchAMD64,
OS: utils.OSLinux,
},
},
},
"add nil node": {
input: nil,
expOperationOutput: errors.New("passed node is nil"),
expResult: make(map[string]*nodeMetadata),
},
"add node with no architecture label": {
input: []*corev1.Node{
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
corev1.LabelOSStable: "linux",
},
},
},
},
expOperationOutput: fmt.Errorf("missing \"kubernetes.io/arch\" label on node \"node1\""),
expResult: make(map[string]*nodeMetadata),
},
"add node with no os label": {
input: []*corev1.Node{
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
corev1.LabelArchStable: "amd64",
},
},
},
},
expOperationOutput: fmt.Errorf("missing %q label on node \"node1\"", corev1.LabelOSStable),
expResult: make(map[string]*nodeMetadata),
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
var nodeMap = New()
for _, node := range test.input {
err := nodeMap.Add(node)
if !reflect.DeepEqual(test.expOperationOutput, err) {
t.Errorf("got unexpected operation error, exp=%s act=%s", test.expOperationOutput, err)
}
}
if !reflect.DeepEqual(nodeMap.nodes, test.expResult) {
t.Errorf("got unexpected result, exp=%#+v got=%#+v", test.expResult, nodeMap.nodes)
}

})
}
}

func TestDelete(t *testing.T) {
tests := map[string]struct {
mapInitialState []*corev1.Node
input string
expOperationOutput error
expResult map[string]*nodeMetadata
}{
"delete valid node": {
mapInitialState: []*corev1.Node{
utils.CreateNode("node1", utils.ArchAMD64, utils.OSLinux),
utils.CreateNode("node2", utils.ArchARM, utils.OSLinux),
},
input: "node1",
expOperationOutput: nil,
expResult: map[string]*nodeMetadata{
"node2": &nodeMetadata{
OS: utils.OSLinux,
Architecture: utils.ArchARM,
},
},
},
"delete empty node name": {
mapInitialState: []*corev1.Node{
utils.CreateNode("node1", utils.ArchAMD64, utils.OSLinux),
utils.CreateNode("node2", utils.ArchARM, utils.OSLinux),
},
input: "",
expOperationOutput: nil,
expResult: map[string]*nodeMetadata{
"node1": &nodeMetadata{
OS: utils.OSLinux,
Architecture: utils.ArchAMD64,
},
"node2": &nodeMetadata{
OS: utils.OSLinux,
Architecture: utils.ArchARM,
},
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
var nodeMap = New()
for _, node := range test.mapInitialState {
err := nodeMap.Add(node)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
}

nodeMap.Delete(test.input)
if !reflect.DeepEqual(nodeMap.nodes, test.expResult) {
t.Errorf("got unexpected result, exp=%#+v got=%#+v", test.expResult, nodeMap.nodes)
}

})
}
}
33 changes: 30 additions & 3 deletions pkg/controller/checker/checker.go → pkg/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,61 @@ package checker

import (
"context"
"errors"
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"

"github.com/jetstack/version-checker/pkg/api"
"github.com/jetstack/version-checker/pkg/controller/search"
"github.com/jetstack/version-checker/pkg/version/semver"
"github.com/jetstack/version-checker/pkg/checker/architecture"
"github.com/jetstack/version-checker/pkg/checker/search"
"github.com/jetstack/version-checker/pkg/checker/version/semver"
"github.com/sirupsen/logrus"
)

type Checker struct {
search search.Searcher
nodes *architecture.NodeMap
}

type Result struct {
CurrentVersion string
LatestVersion string
IsLatest bool
ImageURL string
OS api.OS
Architecture api.Architecture
}

func New(search search.Searcher) *Checker {
const (
nodeMissingMetadata = "error fetching node architecture"
)

func New(search search.Searcher, nodesArchInfo *architecture.NodeMap) *Checker {
return &Checker{
search: search,
nodes: nodesArchInfo,
}
}

// Container will return the result of the given container's current version, compared to the latest upstream
func (c *Checker) Container(ctx context.Context, log *logrus.Entry,
pod *corev1.Pod, container *corev1.Container, opts *api.Options) (*Result, error) {

if opts == nil {
// create new options if nil
opts = new(api.Options)
}
// Get information about the pod node architecture
arch, ok := c.nodes.GetArchitecture(pod.Spec.NodeName)
if ok {
opts.OS = &arch.OS
opts.Architecture = &arch.Architecture
} else {
return nil, errors.New(nodeMissingMetadata)
}

// If the container image SHA status is not ready yet, exit early
statusSHA := containerStatusImageSHA(pod, container.Name)
if len(statusSHA) == 0 {
Expand Down Expand Up @@ -90,6 +113,8 @@ func (c *Checker) Container(ctx context.Context, log *logrus.Entry,
LatestVersion: latestVersion,
IsLatest: isLatest,
ImageURL: imageURL,
OS: latestImage.OS,
Architecture: latestImage.Architecture,
}, nil
}

Expand Down Expand Up @@ -161,6 +186,8 @@ func (c *Checker) isLatestSHA(ctx context.Context, imageURL, currentSHA string,
LatestVersion: latestVersion,
IsLatest: isLatest,
ImageURL: imageURL,
OS: latestImage.OS,
Architecture: latestImage.Architecture,
}, nil
}

Expand Down
Loading