Skip to content

Commit

Permalink
Add interfaces for Image Watcher (#902)
Browse files Browse the repository at this point in the history
**What this PR does / why we need it**:

**Which issue(s) this PR fixes**:

Fixes #

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
-->
```release-note
NONE
```

This PR was merged by Kapetanios.
  • Loading branch information
nakabonne committed Oct 5, 2020
1 parent f4556ad commit d6b3a5a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["registry.go"],
importpath = "github.com/pipe-cd/pipe/pkg/app/piped/containerregistry",
srcs = ["gcr.go"],
importpath = "github.com/pipe-cd/pipe/pkg/app/piped/imageprovider/gcr",
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package containerregistry watches the configured container registry
// for the new images.
package containerregistry
package gcr
9 changes: 9 additions & 0 deletions pkg/app/piped/imagewatcher/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["watcher.go"],
importpath = "github.com/pipe-cd/pipe/pkg/app/piped/imagewatcher",
visibility = ["//visibility:public"],
deps = ["@org_uber_go_zap//:go_default_library"],
)
80 changes: 80 additions & 0 deletions pkg/app/piped/imagewatcher/watcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package imagewatcher provides a piped component
// that periodically checks the image registry and updates
// the image if there are differences with Git.
package imagewatcher

import (
"context"
"time"

"go.uber.org/zap"
)

type Watcher interface {
Run(context.Context) error
}

type watcher struct {
timer *time.Timer
logger *zap.Logger
}

type imageRepos map[string]imageRepo
type imageRepo struct {
}

func NewWatcher(interval time.Duration, logger *zap.Logger) Watcher {
return &watcher{
timer: time.NewTimer(interval),
logger: logger,
}
}

func (w *watcher) Run(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case <-w.timer.C:
reposInReg := w.fetchFromRegistry()
reposInGit := w.fetchFromGit()
outdated := calculateChanges(reposInReg, reposInGit)
if err := w.update(outdated); err != nil {
w.logger.Error("failed to update image", zap.Error(err))
}
}
}
return nil
}

func (w *watcher) fetchFromRegistry() imageRepos {
return nil
}

func (w *watcher) fetchFromGit() imageRepos {
return nil
}

func (w *watcher) update(targets imageRepos) error {
return nil
}

// calculateChanges compares between image repos in the image registry and
// image repos in git. And then gives back image repos to be updated.
func calculateChanges(x, y imageRepos) imageRepos {
return nil
}
1 change: 0 additions & 1 deletion pkg/model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ go_test(
data = glob(["testdata/**"]),
embed = [":go_default_library"],
deps = [
"//pkg/crypto:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
"@org_golang_x_crypto//bcrypt:go_default_library",
],
Expand Down

0 comments on commit d6b3a5a

Please sign in to comment.