Integrate images into a providers layer

This commit is contained in:
CrazyMax
2019-12-11 22:30:35 +01:00
parent 06d8974fe6
commit a2bfb6534e
8 changed files with 155 additions and 152 deletions

View File

@@ -5,6 +5,7 @@ import (
"time"
"github.com/crazy-max/diun/internal/model"
"github.com/crazy-max/diun/internal/model/provider"
"github.com/crazy-max/diun/internal/utl"
"github.com/crazy-max/diun/pkg/docker"
"github.com/crazy-max/diun/pkg/docker/registry"
@@ -12,20 +13,21 @@ import (
)
type imageJob struct {
image model.Image
image provider.Image
registry *docker.RegistryClient
}
func (di *Diun) procImages() {
// Iterate images
for _, img := range di.cfg.Image {
for _, img := range di.cfg.Providers.Image {
regOpts := di.cfg.RegOpts[img.RegOptsID]
reg, err := docker.NewRegistryClient(docker.RegistryOptions{
Os: img.Os,
Arch: img.Arch,
Username: img.RegOpts.Username,
Password: img.RegOpts.Password,
Timeout: time.Duration(img.RegOpts.Timeout) * time.Second,
InsecureTLS: img.RegOpts.InsecureTLS,
Username: regOpts.Username,
Password: regOpts.Password,
Timeout: time.Duration(regOpts.Timeout) * time.Second,
InsecureTLS: regOpts.InsecureTLS,
})
if err != nil {
log.Error().Err(err).Str("image", img.Name).Msg("Cannot create registry client")
@@ -102,8 +104,6 @@ func (di *Diun) imageJob(job imageJob) error {
if err != nil {
return err
}
/*b, _ := json.MarshalIndent(liveManifest, "", " ")
log.Debug().Msg(string(b))*/
dbManifest, err := di.db.GetManifest(image)
if err != nil {

View File

@@ -11,6 +11,7 @@ import (
"regexp"
"github.com/crazy-max/diun/internal/model"
"github.com/crazy-max/diun/internal/model/provider"
"github.com/crazy-max/diun/internal/utl"
"github.com/imdario/mergo"
"github.com/rs/zerolog/log"
@@ -19,13 +20,13 @@ import (
// Config holds configuration details
type Config struct {
Flags model.Flags
App model.App
Db model.Db `yaml:"db,omitempty"`
Watch model.Watch `yaml:"watch,omitempty"`
Notif model.Notif `yaml:"notif,omitempty"`
RegOpts map[string]model.RegOpts `yaml:"regopts,omitempty"`
Image []model.Image `yaml:"image,omitempty"`
Flags model.Flags
App model.App
Db model.Db `yaml:"db,omitempty"`
Watch model.Watch `yaml:"watch,omitempty"`
Notif model.Notif `yaml:"notif,omitempty"`
RegOpts map[string]model.RegOpts `yaml:"regopts,omitempty"`
Providers model.Providers `yaml:"providers,omitempty"`
}
// Load returns Configuration struct
@@ -62,6 +63,9 @@ func Load(flags model.Flags, version string) (*Config, error) {
Timeout: 10,
},
},
Providers: model.Providers{
Image: []provider.Image{},
},
}
if _, err = os.Lstat(flags.Cfgfile); err != nil {
@@ -97,7 +101,7 @@ func (cfg *Config) validate() error {
}
}
for key, img := range cfg.Image {
for key, img := range cfg.Providers.Image {
if err := cfg.validateImage(key, img); err != nil {
return err
}
@@ -132,12 +136,12 @@ func (cfg *Config) validateRegOpts(id string, regopts model.RegOpts) error {
return nil
}
func (cfg *Config) validateImage(key int, img model.Image) error {
func (cfg *Config) validateImage(key int, img provider.Image) error {
if img.Name == "" {
return fmt.Errorf("name is required for image %d", key)
}
if err := mergo.Merge(&img, model.Image{
if err := mergo.Merge(&img, provider.Image{
Os: "linux",
Arch: "amd64",
WatchRepo: false,
@@ -147,11 +151,10 @@ func (cfg *Config) validateImage(key int, img model.Image) error {
}
if img.RegOptsID != "" {
regopts, found := cfg.RegOpts[img.RegOptsID]
_, found := cfg.RegOpts[img.RegOptsID]
if !found {
return fmt.Errorf("registry options %s not found for %s", img.RegOptsID, img.Name)
}
img.RegOpts = regopts
}
for _, includeTag := range img.IncludeTags {
@@ -166,7 +169,7 @@ func (cfg *Config) validateImage(key int, img model.Image) error {
}
}
cfg.Image[key] = img
cfg.Providers.Image[key] = img
return nil
}

View File

@@ -32,45 +32,34 @@ regopts:
username: foo
password: bar
image:
-
name: docker.io/crazymax/nextcloud:latest
regopts_id: someregopts
-
name: crazymax/swarm-cronjob
watch_repo: true
include_tags:
- ^1\.2\..*
-
name: jfrog-docker-reg2.bintray.io/jfrog/artifactory-oss:4.0.0
regopts_id: bintrayoptions
-
name: docker.bintray.io/jfrog/xray-server:2.8.6
watch_repo: true
max_tags: 50
-
name: quay.io/coreos/hyperkube
-
name: docker.io/portainer/portainer
watch_repo: true
max_tags: 10
include_tags:
- ^(0|[1-9]\d*)\..*
-
name: traefik
watch_repo: true
-
name: alpine
os: linux
arch: arm64v8
-
name: docker.io/graylog/graylog:3.2.0
-
name: jacobalberty/unifi:5.9
-
name: quay.io/coreos/hyperkube:v1.1.7-coreos.1
-
name: crazymax/ddns-route53
watch_repo: true
include_tags:
- ^1\..*
providers:
image:
- name: docker.io/crazymax/nextcloud:latest
regopts_id: someregopts
- name: crazymax/swarm-cronjob
watch_repo: true
include_tags:
- ^1\.2\..*
- name: jfrog-docker-reg2.bintray.io/jfrog/artifactory-oss:4.0.0
regopts_id: bintrayoptions
- name: docker.bintray.io/jfrog/xray-server:2.8.6
watch_repo: true
max_tags: 50
- name: quay.io/coreos/hyperkube
- name: docker.io/portainer/portainer
watch_repo: true
max_tags: 10
include_tags:
- ^(0|[1-9]\d*)\..*
- name: traefik
watch_repo: true
- name: alpine
os: linux
arch: arm64v8
- name: docker.io/graylog/graylog:3.2.0
- name: jacobalberty/unifi:5.9
- name: quay.io/coreos/hyperkube:v1.1.7-coreos.1
- name: crazymax/ddns-route53
watch_repo: true
include_tags:
- ^1\..*

View File

@@ -5,6 +5,7 @@ import (
"github.com/crazy-max/diun/internal/config"
"github.com/crazy-max/diun/internal/model"
"github.com/crazy-max/diun/internal/model/provider"
"github.com/stretchr/testify/assert"
)
@@ -79,83 +80,85 @@ func TestLoad(t *testing.T) {
Password: "bar",
},
},
Image: []model.Image{
{
Name: "docker.io/crazymax/nextcloud:latest",
Os: "linux",
Arch: "amd64",
RegOptsID: "someregopts",
},
{
Name: "crazymax/swarm-cronjob",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
IncludeTags: []string{
`^1\.2\..*`,
Providers: model.Providers{
Image: []provider.Image{
{
Name: "docker.io/crazymax/nextcloud:latest",
Os: "linux",
Arch: "amd64",
RegOptsID: "someregopts",
},
},
{
Name: "jfrog-docker-reg2.bintray.io/jfrog/artifactory-oss:4.0.0",
Os: "linux",
Arch: "amd64",
RegOptsID: "bintrayoptions",
},
{
Name: "docker.bintray.io/jfrog/xray-server:2.8.6",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
MaxTags: 50,
},
{
Name: "quay.io/coreos/hyperkube",
Os: "linux",
Arch: "amd64",
},
{
Name: "docker.io/portainer/portainer",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
MaxTags: 10,
IncludeTags: []string{
`^(0|[1-9]\d*)\..*`,
{
Name: "crazymax/swarm-cronjob",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
IncludeTags: []string{
`^1\.2\..*`,
},
},
},
{
Name: "traefik",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
},
{
Name: "alpine",
Os: "linux",
Arch: "arm64v8",
},
{
Name: "docker.io/graylog/graylog:3.2.0",
Os: "linux",
Arch: "amd64",
},
{
Name: "jacobalberty/unifi:5.9",
Os: "linux",
Arch: "amd64",
},
{
Name: "quay.io/coreos/hyperkube:v1.1.7-coreos.1",
Os: "linux",
Arch: "amd64",
},
{
Name: "crazymax/ddns-route53",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
IncludeTags: []string{
`^1\..*`,
{
Name: "jfrog-docker-reg2.bintray.io/jfrog/artifactory-oss:4.0.0",
Os: "linux",
Arch: "amd64",
RegOptsID: "bintrayoptions",
},
{
Name: "docker.bintray.io/jfrog/xray-server:2.8.6",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
MaxTags: 50,
},
{
Name: "quay.io/coreos/hyperkube",
Os: "linux",
Arch: "amd64",
},
{
Name: "docker.io/portainer/portainer",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
MaxTags: 10,
IncludeTags: []string{
`^(0|[1-9]\d*)\..*`,
},
},
{
Name: "traefik",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
},
{
Name: "alpine",
Os: "linux",
Arch: "arm64v8",
},
{
Name: "docker.io/graylog/graylog:3.2.0",
Os: "linux",
Arch: "amd64",
},
{
Name: "jacobalberty/unifi:5.9",
Os: "linux",
Arch: "amd64",
},
{
Name: "quay.io/coreos/hyperkube:v1.1.7-coreos.1",
Os: "linux",
Arch: "amd64",
},
{
Name: "crazymax/ddns-route53",
Os: "linux",
Arch: "amd64",
WatchRepo: true,
IncludeTags: []string{
`^1\..*`,
},
},
},
},

View File

@@ -1,14 +1,6 @@
package model
package provider
// RegOpts holds registry options configuration
type RegOpts struct {
Username string `yaml:"username,omitempty" json:",omitempty"`
Password string `yaml:"password,omitempty" json:",omitempty"`
InsecureTLS bool `yaml:"insecure_tls,omitempty" json:",omitempty"`
Timeout int `yaml:"timeout,omitempty" json:",omitempty"`
}
// Image holds image configuration
// Image holds image provider configuration
type Image struct {
Name string `yaml:"name,omitempty" json:",omitempty"`
Os string `yaml:"os,omitempty" json:",omitempty"`
@@ -18,5 +10,4 @@ type Image struct {
MaxTags int `yaml:"max_tags,omitempty" json:",omitempty"`
IncludeTags []string `yaml:"include_tags,omitempty" json:",omitempty"`
ExcludeTags []string `yaml:"exclude_tags,omitempty" json:",omitempty"`
RegOpts RegOpts `yaml:"-" json:"-"`
}

View File

@@ -0,0 +1,8 @@
package model
import "github.com/crazy-max/diun/internal/model/provider"
// Providers represents a provider configuration
type Providers struct {
Image []provider.Image `yaml:"image,omitempty" json:",omitempty"`
}

View File

@@ -0,0 +1,9 @@
package model
// RegOpts holds registry options configuration
type RegOpts struct {
Username string `yaml:"username,omitempty" json:",omitempty"`
Password string `yaml:"password,omitempty" json:",omitempty"`
InsecureTLS bool `yaml:"insecure_tls,omitempty" json:",omitempty"`
Timeout int `yaml:"timeout,omitempty" json:",omitempty"`
}

View File

@@ -1,4 +1,4 @@
package _dev
package docker
import (
"context"
@@ -8,7 +8,7 @@ import (
// Client represents an active docker object
type Client struct {
Cli *client.Client
Api *client.Client
}
// NewEnvClient initializes a new Docker API client based on environment variables
@@ -23,5 +23,5 @@ func NewEnvClient() (*Client, error) {
return nil, err
}
return &Client{Cli: d}, err
return &Client{Api: d}, err
}