Skip to content

Commit

Permalink
cleanup: moved some k8s logics to libk8soci
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kotzbauer <[email protected]>
  • Loading branch information
ckotzbauer committed Jun 16, 2022
1 parent e87fbf1 commit 3764b74
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 188 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ jobs:
- uses: codecov/codecov-action@v3
with:
files: cover-oci.out,cover-registry.out
files: cover-oci.out
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ vet:
go vet ./...

test:
go test $(shell go list ./... | grep -v sbom-operator/internal/registry | grep -v sbom-operator/internal/target/oci) -coverprofile cover.out
go test $(shell go list ./... | grep -v sbom-operator/internal/target/oci) -coverprofile cover.out

test-integration:
bash internal/target/oci/fixtures/oci-test.sh $(REGISTRY_USER) $(REGISTRY_TOKEN)
go test github.com/ckotzbauer/sbom-operator/internal/registry -coverprofile cover-registry.out

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/anchore/syft v0.47.0
github.com/ckotzbauer/libk8soci v0.0.0-20220616062755-33820a752d05
github.com/ckotzbauer/libk8soci v0.0.0-20220616071828-a85971a56e7e
github.com/novln/docker-parser v1.0.0
github.com/nscuro/dtrack-client v0.5.0
github.com/robfig/cron v1.2.0
Expand All @@ -14,7 +14,6 @@ require (
github.com/stretchr/testify v1.7.2
k8s.io/api v0.24.1
k8s.io/apimachinery v0.24.1
k8s.io/client-go v0.24.1
)

require (
Expand Down Expand Up @@ -111,6 +110,7 @@ require (
google.golang.org/grpc v1.46.2 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
k8s.io/client-go v0.24.1 // indirect
lukechampine.com/uint128 v1.1.1 // indirect
modernc.org/cc/v3 v3.35.22 // indirect
modernc.org/ccgo/v3 v3.15.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ github.com/cilium/ebpf v0.0.0-20200702112145-1c8d4c9ef775/go.mod h1:7cR51M8ViRLI
github.com/cilium/ebpf v0.2.0/go.mod h1:To2CFviqOWL/M0gIMsvSMlqe7em/l1ALkX1PyjrX2Qs=
github.com/cilium/ebpf v0.4.0/go.mod h1:4tRaxcgiL706VnOzHOdBlY8IEAIdxINsQBcU4xJJXRs=
github.com/cilium/ebpf v0.6.2/go.mod h1:4tRaxcgiL706VnOzHOdBlY8IEAIdxINsQBcU4xJJXRs=
github.com/ckotzbauer/libk8soci v0.0.0-20220616062755-33820a752d05 h1:tkzw6L5Yxd8Tl6FMgeTKo/Rl9LrjvnKQzF9tCiCbzPo=
github.com/ckotzbauer/libk8soci v0.0.0-20220616062755-33820a752d05/go.mod h1:bCO4JZ7vub7ma+5Civ6GJrIH4Qi3HOcY7h+7L4KRuec=
github.com/ckotzbauer/libk8soci v0.0.0-20220616071828-a85971a56e7e h1:lchNRzLhlooLdnwZReZSlyJhO8ffJ8K1JvEqRvJoHys=
github.com/ckotzbauer/libk8soci v0.0.0-20220616071828-a85971a56e7e/go.mod h1:MuZMhEXIeKexg2qhJVtYc3xNo15g63M9d1ArjKSCXU4=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down
13 changes: 7 additions & 6 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
import (
"time"

libk8s "github.com/ckotzbauer/libk8soci/pkg/kubernetes"
"github.com/ckotzbauer/sbom-operator/internal"
"github.com/ckotzbauer/sbom-operator/internal/job"
"github.com/ckotzbauer/sbom-operator/internal/kubernetes"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (c *CronService) runBackgroundService() {

k8s := kubernetes.NewClient(viper.GetBool(internal.ConfigKeyIgnoreAnnotations))
namespaceSelector := viper.GetString(internal.ConfigKeyNamespaceLabelSelector)
namespaces, err := k8s.ListNamespaces(namespaceSelector)
namespaces, err := k8s.Client.ListNamespaces(namespaceSelector)
if err != nil {
logrus.WithError(err).Errorf("failed to list namespaces with selector: %s, abort background-service", namespaceSelector)
return
Expand All @@ -90,11 +91,11 @@ func (c *CronService) runBackgroundService() {
}

func (c *CronService) executeSyftScans(format string, k8s *kubernetes.KubeClient,
containerImages map[string]kubernetes.ContainerImage, allImages []kubernetes.ContainerImage) {
containerImages []libk8s.KubeImage, allImages map[string]libk8s.KubeImage) {
sy := syft.New(format)

for _, image := range containerImages {
sbom, err := sy.ExecuteSyft(image)
sbom, err := sy.ExecuteSyft(image.Image)
if err != nil {
// Error is already handled from syft module.
continue
Expand All @@ -114,12 +115,12 @@ func (c *CronService) executeSyftScans(format string, k8s *kubernetes.KubeClient
}
}

for _, t := range c.targets {
/*for _, t := range c.targets {
t.Cleanup(allImages)
}
}*/
}

func executeJobImage(k8s *kubernetes.KubeClient, containerImages map[string]kubernetes.ContainerImage) {
func executeJobImage(k8s *kubernetes.KubeClient, containerImages []libk8s.KubeImage) {
jobClient := job.New(
k8s,
viper.GetString(internal.ConfigKeyJobImage),
Expand Down
9 changes: 5 additions & 4 deletions internal/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
corev1 "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"

libk8s "github.com/ckotzbauer/libk8soci/pkg/kubernetes"
"github.com/ckotzbauer/libk8soci/pkg/oci"
"github.com/ckotzbauer/sbom-operator/internal/kubernetes"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -48,12 +49,12 @@ func New(k8s *kubernetes.KubeClient, image, imagePullSecret, clusterId string, t
}
}

func (j JobClient) StartJob(images map[string]kubernetes.ContainerImage) (*batchv1.Job, error) {
func (j JobClient) StartJob(images []libk8s.KubeImage) (*batchv1.Job, error) {
configs := make([]imageConfig, 0)
podNamespace := os.Getenv("POD_NAMESPACE")

for _, image := range images {
cfg, err := oci.ResolveAuthConfig(oci.RegistryImage{ImageID: image.ImageID, PullSecrets: image.PullSecrets})
cfg, err := oci.ResolveAuthConfig(oci.RegistryImage{ImageID: image.Image.ImageID, PullSecrets: image.Image.PullSecrets})
if err != nil {
logrus.WithError(err).Error("Error occurred during auth-resolve")
return nil, err
Expand All @@ -63,7 +64,7 @@ func (j JobClient) StartJob(images map[string]kubernetes.ContainerImage) (*batch
Host: cfg.ServerAddress,
User: cfg.Username,
Password: cfg.Password,
Image: image.ImageID,
Image: image.Image.ImageID,
Pods: j.convertPods(image.Pods),
})
}
Expand Down Expand Up @@ -93,7 +94,7 @@ func (j JobClient) StartJob(images map[string]kubernetes.ContainerImage) (*batch

func (j JobClient) WaitForJob(job *batchv1.Job) bool {
for {
job, err := j.k8s.Client.BatchV1().Jobs(job.Namespace).Get(context.Background(), job.Name, meta.GetOptions{})
job, err := j.k8s.Client.Client.BatchV1().Jobs(job.Namespace).Get(context.Background(), job.Name, meta.GetOptions{})
if err != nil {
logrus.WithError(err).Warnf("Error while waiting for job %s.", job.Name)
return false
Expand Down
Loading

0 comments on commit 3764b74

Please sign in to comment.