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

Add command to force tags on nodes and list tags #558

Merged
merged 51 commits into from
May 16, 2022
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
02f68eb
feat: add forcedTags field and update proto
restanrm Apr 15, 2022
9de9bc2
feat(cli): add tag subcommand to add and remove tags
restanrm Apr 15, 2022
cd1d107
feat(acls): add support for forced tags
restanrm Apr 15, 2022
98f54c9
chore: apply format and lint
restanrm Apr 15, 2022
587bdc7
feat: add valid and invalid fields
restanrm Apr 16, 2022
db1528b
feat: add invalid and valid tags to grpc response
restanrm Apr 16, 2022
89a1a56
feat: add unit tests and fmt
restanrm Apr 16, 2022
4fcc5e2
chore: fmt for grpc file
restanrm Apr 16, 2022
4651c44
feat: print tags in nodes list
restanrm Apr 16, 2022
ea7bcff
Merge branch 'main' into feat-list-tags-of-machines
restanrm Apr 21, 2022
f53bb63
fix: move tag command to subcommand of nodes
restanrm Apr 21, 2022
17d6624
chore: fix lint
restanrm Apr 21, 2022
b9fee36
fix: linting
restanrm Apr 21, 2022
3a90079
Merge branch 'main' into feat-list-tags-of-machines
kradalby Apr 22, 2022
31debf7
feat: rewrite proto to only update tags of machine
restanrm Apr 25, 2022
25f1dcf
feat: update generated files
restanrm Apr 25, 2022
cc9eeda
feat: updating cli to match the set command
restanrm Apr 25, 2022
ea9aaa6
feat: update functions to use set command
restanrm Apr 25, 2022
8061abe
refact: use generics for contains functions
restanrm Apr 25, 2022
3d30244
fix: order error in the tests
restanrm Apr 25, 2022
2c448d4
chore: apply linting
restanrm Apr 25, 2022
fec8cda
fix: fix linting issue on my computer
restanrm Apr 25, 2022
a2fb5b2
Merge remote-tracking branch 'origin/main' into feat-list-tags-of-mac…
restanrm May 3, 2022
68417cc
fix(go): add missing updated files
restanrm May 3, 2022
dc8c20e
fix: handle empty aclPolicy for integration tests
restanrm May 4, 2022
ad4401a
fix: remove debug code
restanrm May 13, 2022
fdbc965
feat: return error if validation is failed
restanrm May 13, 2022
62cfd60
feat: add validation of tags
restanrm May 13, 2022
209d003
feat: handle insert into database error
restanrm May 13, 2022
16f9691
fix: ignore emptyPolicy errors for db insertion
restanrm May 13, 2022
63d9205
feat: improve nodes list with inputs from @deonthomasgy
restanrm May 13, 2022
31c0062
feat: add integration tests for tag support
restanrm May 13, 2022
294ed7a
docs: update changelog
restanrm May 13, 2022
72c1eda
Merge remote-tracking branch 'origin/main' into feat-list-tags-of-mac…
restanrm May 13, 2022
49ec994
fix: loop over result machines instead of startup machines
restanrm May 13, 2022
09836cd
chore: update vendorSha after update of go.mod and go.sum
restanrm May 13, 2022
fcdc292
fix: update tag in db if acl is enabled
restanrm May 13, 2022
b511295
fix: integration tests result
restanrm May 13, 2022
a28eebf
Merge branch 'main' into feat-list-tags-of-machines
kradalby May 15, 2022
ca71830
docs: add small documentation on getTags func
restanrm May 16, 2022
bc1909f
Merge branch 'feat-list-tags-of-machines' of github.com:restanrm/head…
restanrm May 16, 2022
0445f40
fix: pin version of golangci-lint in GA
restanrm May 16, 2022
522e892
fix: remove unknown linters:
restanrm May 16, 2022
844ad15
fix: revert previous commit and add exclusion of linter
restanrm May 16, 2022
852dc0f
feat: add golangci-lint in nix develop
restanrm May 16, 2022
02ae7a0
fix: pin version of golangci-lint to match dev config
restanrm May 16, 2022
4435a4f
chore: apply lint recommendations
restanrm May 16, 2022
c4e69fe
fix: ignore exhaust linter
restanrm May 16, 2022
1158210
fix: flake.nex update sha256
restanrm May 16, 2022
9f08212
fix: remove version pinning for golangci-lint it does not work
restanrm May 16, 2022
c9efd5c
Merge branch 'main' into feat-list-tags-of-machines
kradalby May 16, 2022
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
208 changes: 208 additions & 0 deletions cmd/headscale/cli/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package cli

import (
"fmt"
"log"

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(tagCmd)

addTagCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
err := addTagCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
}
addTagCmd.Flags().StringSliceP("tags", "t", []string{}, "List of tags to add to the node")
tagCmd.AddCommand(addTagCmd)

delTagCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
err = delTagCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
}
delTagCmd.Flags().StringSliceP("tags", "t", []string{}, "List of tags to remove from the node")
tagCmd.AddCommand(delTagCmd)
}

var tagCmd = &cobra.Command{
Use: "tags",
Short: "Manage the tags of Headscale",
Aliases: []string{"t", "tag"},
}

var addTagCmd = &cobra.Command{
Use: "add",
restanrm marked this conversation as resolved.
Show resolved Hide resolved
Short: "Add tags to a node in your network",
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()

// retrieve flags from CLI
identifier, err := cmd.Flags().GetUint64("identifier")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error converting ID to integer: %s", err),
output,
)

return
}
tagsToAdd, err := cmd.Flags().GetStringSlice("tags")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error retrieving list of tags to add to machine", err),
output,
)

return
}

// retrieve machine informations
request := &v1.GetMachineRequest{
MachineId: identifier,
}
resp, err := client.GetMachine(ctx, request)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error retrieving machine: %s", err),
output,
)
}

// update machine
mergedTags := resp.Machine.GetForcedTags()
for _, tag := range tagsToAdd {
if !containsString(mergedTags, tag) {
mergedTags = append(mergedTags, tag)
}
}

machine := resp.GetMachine()
machine.ForcedTags = mergedTags

updateReq := &v1.UpdateMachineRequest{
Machine: machine,
}

// send updated machine upstream
updateResponse, err := client.UpdateMachine(ctx, updateReq)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error while updating machine: %s", err),
output,
)
}

if updateResponse != nil {
SuccessOutput(
updateResponse.GetMachine(),
"Machine updated",
output,
)
}

},
}

var delTagCmd = &cobra.Command{
Use: "del",
Short: "remove tags to a node in your network",
Aliases: []string{"remove", "rm"},
restanrm marked this conversation as resolved.
Show resolved Hide resolved
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()

// retrieve flags from CLI
identifier, err := cmd.Flags().GetUint64("identifier")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error converting ID to integer: %s", err),
output,
)

return
}
tagsToRemove, err := cmd.Flags().GetStringSlice("tags")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error retrieving list of tags to add to machine", err),
output,
)

return
}

// retrieve machine informations
request := &v1.GetMachineRequest{
MachineId: identifier,
}
resp, err := client.GetMachine(ctx, request)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error retrieving machine: %s", err),
output,
)
}

// update machine
keepTags := resp.Machine.GetForcedTags()
for _, tag := range tagsToRemove {
for i, t := range keepTags {
if t == tag {
keepTags = append(keepTags[:i], keepTags[i+1:]...)
}
}
}

machine := resp.GetMachine()
machine.ForcedTags = keepTags

updateReq := &v1.UpdateMachineRequest{
Machine: machine,
}

// send updated machine upstream
updateResponse, err := client.UpdateMachine(ctx, updateReq)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error while updating machine: %s", err),
output,
)
}

if updateResponse != nil {
SuccessOutput(
updateResponse.GetMachine(),
"Machine updated",
output,
)
}

},
}

func containsString(ss []string, s string) bool {
restanrm marked this conversation as resolved.
Show resolved Hide resolved
for _, v := range ss {
if v == s {
return true
}
}

return false
}