Skip to content

Commit

Permalink
feat: implement namespaces, clean up context use
Browse files Browse the repository at this point in the history
More of general cleanup/refactoring, no new features.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
  • Loading branch information
smira authored and talos-bot committed Nov 18, 2020
1 parent 81bf414 commit f450ab7
Show file tree
Hide file tree
Showing 24 changed files with 444 additions and 172 deletions.
34 changes: 34 additions & 0 deletions .conform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-09-25T18:15:38Z by kres ce6bee5-dirty.

policies:
- type: commit
spec:
dco: true
gpg: false
spellcheck:
locale: US
maximumOfOneCommit: true
header:
length: 89
imperative: true
case: lower
invalidLastCharacters: .
body:
required: true
conventional:
types: ["chore","docs","perf","refactor","style","test","release"]
scopes: ["*"]
- type: license
spec:
skipPaths:
- .git/
includeSuffixes:
- .go
excludeSuffixes:
- .pb.go
header: |
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
3 changes: 1 addition & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-08-28T20:50:38Z by kres 292ed36-dirty.
# Generated on 2020-11-17T20:11:41Z by kres eb337ab.

kind: pipeline
type: kubernetes
Expand Down Expand Up @@ -166,7 +166,6 @@ services:
- --dns=8.8.4.4
- --mtu=1500
- --log-level=error
- --insecure-registry=http://registry.ci.svc:5000
privileged: true
volumes:
- name: outer-docker-socket
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-08-28T20:51:32Z by kres 292ed36-dirty.
# Generated on 2020-09-25T18:15:38Z by kres ce6bee5-dirty.

# common variables

SHA := $(shell git describe --match=none --always --abbrev=8 --dirty)
TAG := $(shell git describe --tag --always --dirty)
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
ARTIFACTS := _out
REGISTRY ?= docker.io
USERNAME ?= autonomy
REGISTRY ?= ghcr.io
USERNAME ?= talos-systems
REGISTRY_AND_USERNAME ?= $(REGISTRY)/$(USERNAME)
GOFUMPT_VERSION ?= abc0db2c416aca0f60ea33c23c76665f6e7ba0b6
GO_VERSION ?= 1.14
TESTPKGS ?= ./...
KRES_IMAGE ?= autonomy/kres:latest
KRES_IMAGE ?= ghcr.io/talos-systems/kres:latest

# docker build settings

Expand All @@ -34,7 +34,7 @@ COMMON_ARGS += --build-arg=USERNAME=$(USERNAME)
COMMON_ARGS += --build-arg=TOOLCHAIN=$(TOOLCHAIN)
COMMON_ARGS += --build-arg=GOFUMPT_VERSION=$(GOFUMPT_VERSION)
COMMON_ARGS += --build-arg=TESTPKGS=$(TESTPKGS)
TOOLCHAIN ?= docker.io/golang:1.14-alpine
TOOLCHAIN ?= docker.io/golang:1.15-alpine

# help menu

Expand Down Expand Up @@ -130,7 +130,7 @@ lint: lint-golangci-lint lint-gofumpt lint-markdown ## Run all linters for the
.PHONY: rekres
rekres:
@docker pull $(KRES_IMAGE)
@docker run --rm -v $(PWD):/src -w /src $(KRES_IMAGE)
@docker run --rm -v $(PWD):/src -w /src -e GITHUB_TOKEN $(KRES_IMAGE)

.PHONY: help
help: ## This help menu.
Expand Down
42 changes: 26 additions & 16 deletions cmd/directory-fun/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package main

import (
Expand Down Expand Up @@ -30,7 +34,7 @@ func DirectoryTask(world state.State, path string) {
err error
)

if parent, err = world.WatchFor(ctx, PathResourceType, base, state.WithEventTypes(state.Created, state.Updated)); err != nil {
if parent, err = world.WatchFor(ctx, resource.NewMetadata(defaultNs, PathResourceType, base, resource.VersionUndefined), state.WithEventTypes(state.Created, state.Updated)); err != nil {
log.Fatal(err)
}

Expand All @@ -40,43 +44,46 @@ func DirectoryTask(world state.State, path string) {
log.Fatal(err)
}

self := NewPathResource(path)
self := NewPathResource(defaultNs, path)

if err = world.Create(self); err != nil {
if err = world.Create(ctx, self); err != nil {
log.Fatal(err)
}

log.Printf("%q: created %q", path, path)

if parent, err = world.UpdateWithConflicts(parent, func(r resource.Resource) error {
if parent, err = world.UpdateWithConflicts(ctx, parent, func(r resource.Resource) error {
r.(*PathResource).AddDependent(self)

return nil
}); err != nil {
log.Fatal(err)
}

log.Printf("%q: %q.dependents = %q", path, parent.ID(), parent.(*PathResource).dependents)
log.Printf("%q: %q.dependents = %q", path, parent.Metadata().ID(), parent.(*PathResource).spec.dependents)

// doing something useful here <>

log.Printf("%q: watching for teardown %q", path, base)

if parent, err = world.WatchFor(ctx, PathResourceType, base, state.WithEventTypes(state.Destroyed, state.Torndown)); err != nil {
if parent, err = world.WatchFor(ctx, resource.NewMetadata(defaultNs, PathResourceType, base, resource.VersionUndefined), state.WithEventTypes(state.Destroyed, state.Torndown)); err != nil {
log.Fatal(err)
}

log.Printf("%q: teardown self", path)

if err = world.Teardown(self); err != nil {
if err = world.Teardown(ctx, self.Metadata()); err != nil {
log.Fatal(err)
}

log.Printf("%q: watching for dependents to vanish %q", path, path)

if _, err = world.WatchFor(ctx, PathResourceType, path, state.WithEventTypes(state.Created, state.Updated, state.Torndown), state.WithCondition(func(r resource.Resource) (bool, error) {
return len(r.(*PathResource).dependents) == 0, nil
})); err != nil {
if _, err = world.WatchFor(ctx,
resource.NewMetadata(defaultNs, PathResourceType, path, resource.VersionUndefined),
state.WithEventTypes(state.Created, state.Updated, state.Torndown),
state.WithCondition(func(r resource.Resource) (bool, error) {
return len(r.(*PathResource).spec.dependents) == 0, nil
})); err != nil {
log.Fatal(err)
}

Expand All @@ -86,24 +93,27 @@ func DirectoryTask(world state.State, path string) {
log.Fatal(err)
}

if _, err = world.UpdateWithConflicts(parent, func(r resource.Resource) error {
if _, err = world.UpdateWithConflicts(ctx, parent, func(r resource.Resource) error {
r.(*PathResource).DropDependent(self)

return nil
}); err != nil {
log.Fatal(err)
}

if err = world.Destroy(self); err != nil {
if err = world.Destroy(ctx, self.Metadata()); err != nil {
log.Fatal(err)
}
}

const defaultNs = "default"

func main() {
world := state.WrapCore(local.NewState())
ctx := context.Background()
world := state.WrapCore(local.NewState(defaultNs))

root := NewPathResource(".")
if err := world.Create(root); err != nil {
root := NewPathResource(defaultNs, ".")
if err := world.Create(ctx, root); err != nil {
log.Fatal(err)
}

Expand All @@ -127,7 +137,7 @@ func main() {

time.Sleep(2 * time.Second)

if err := world.Teardown(root); err != nil {
if err := world.Teardown(ctx, root.Metadata()); err != nil {
log.Fatal(err)
}

Expand Down
56 changes: 30 additions & 26 deletions cmd/directory-fun/path.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,74 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//nolint: golint
package main

import (
"fmt"
"strconv"

"github.com/talos-systems/os-runtime/pkg/resource"
)

const PathResourceType = resource.Type("os/path")

type pathResourceSpec struct {
dependents []string
}

// PathResource represents a path in the filesystem.
//
// Resource ID is the path, and dependents are all the immediate
// children on the path.
type PathResource struct {
path string
version int
dependents []string
md resource.Metadata
spec pathResourceSpec
}

func NewPathResource(path string) *PathResource {
return &PathResource{path: path}
}

func (path *PathResource) ID() resource.ID {
return path.path
}
func NewPathResource(ns resource.Namespace, path string) *PathResource {
r := &PathResource{
md: resource.NewMetadata(ns, PathResourceType, path, resource.VersionUndefined),
}
r.md.BumpVersion()

func (path *PathResource) Type() resource.Type {
return PathResourceType
return r
}

func (path *PathResource) Version() resource.Version {
return strconv.Itoa(path.version)
func (path *PathResource) Metadata() resource.Metadata {
return path.md
}

func (path *PathResource) Spec() interface{} {
return nil
return path.spec
}

func (path *PathResource) String() string {
return fmt.Sprintf("PathResource(%q)", path.path)
return fmt.Sprintf("PathResource(%q)", path.md.ID())
}

func (path *PathResource) Copy() resource.Resource {
return &PathResource{
path: path.path,
version: path.version,
dependents: append([]string(nil), path.dependents...),
md: path.md,
spec: pathResourceSpec{
dependents: append([]string(nil), path.spec.dependents...),
},
}
}

func (path *PathResource) AddDependent(dependent *PathResource) {
path.dependents = append(path.dependents, dependent.path)
path.version++
path.spec.dependents = append(path.spec.dependents, dependent.md.ID())
path.md.BumpVersion()
}

func (path *PathResource) DropDependent(dependent *PathResource) {
for i, p := range path.dependents {
if p == dependent.path {
path.dependents = append(path.dependents[:i], path.dependents[i+1:]...)
for i, p := range path.spec.dependents {
if p == dependent.md.ID() {
path.spec.dependents = append(path.spec.dependents[:i], path.spec.dependents[i+1:]...)

break
}
}

path.version++
path.md.BumpVersion()
}
4 changes: 2 additions & 2 deletions hack/git-chglog/config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-08-28T20:43:11Z by kres 292ed36-dirty.
# Generated on 2020-09-25T18:15:38Z by kres ce6bee5-dirty.

style: github
template: CHANGELOG.tpl.md
info:
title: CHANGELOG
repository_url: https://github.com/talos-systems/talos
repository_url: https://github.com/talos-systems/os-runtime
options:
commits:
# filters:
Expand Down
4 changes: 2 additions & 2 deletions hack/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-08-28T20:43:11Z by kres 292ed36-dirty.
# Generated on 2020-09-25T18:15:38Z by kres ce6bee5-dirty.


set -e

function changelog {
if [ "$#" -eq 1 ]; then
git-chglog --output CHANGELOG.md -c ./hack/git-chglog/config.yaml --tag-filter-pattern "^${1}" "${1}.0-alpha.1.."
git-chglog --output CHANGELOG.md -c ./hack/git-chglog/config.yaml --tag-filter-pattern "^${1}" "${1}.0-alpha.0.."
elif [ "$#" -eq 0 ]; then
git-chglog --output CHANGELOG.md -c ./hack/git-chglog/config.yaml
else
Expand Down

0 comments on commit f450ab7

Please sign in to comment.