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

[sabakan] improve sabactl machines get #239

Merged
merged 8 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
7 changes: 5 additions & 2 deletions models/etcd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"

"github.com/cybozu-go/sabakan/v2"
Expand Down Expand Up @@ -152,8 +153,10 @@ func (mi *machinesIndex) query(q sabakan.Query) []string {

res := make(map[string]struct{})

for _, serial := range mi.Rack[q.Rack()] {
res[serial] = struct{}{}
for _, rack := range strings.Split(q.Rack(), ",") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can we only specify multiple racks?
We want to specify other options as well.

The original issue is just an example of a rack.

for _, serial := range mi.Rack[rack] {
res[serial] = struct{}{}
}
}
for _, serial := range mi.Role[q.Role()] {
res[serial] = struct{}{}
Expand Down
2 changes: 1 addition & 1 deletion models/etcd/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (d *driver) machineQuery(ctx context.Context, q sabakan.Query) ([]*sabakan.
var serials []string

switch {
case q.IsEmpty():
case q.IsEmpty() || q.HasOnlyWithout():
resp, err := d.client.Get(ctx, KeyMachines, clientv3.WithPrefix(), clientv3.WithKeysOnly())
if err != nil {
return nil, err
Expand Down
55 changes: 43 additions & 12 deletions pkg/sabactl/cmd/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strings"
"text/tabwriter"
"time"

"github.com/cybozu-go/sabakan/v2"
Expand All @@ -32,6 +33,10 @@ var machinesGetCmd = &cobra.Command{

RunE: func(cmd *cobra.Command, args []string) error {
params := make(map[string]string)
outputFormat, ok := machinesGetParams["output"]
if ok {
delete(machinesGetParams, "output")
}
for k, v := range machinesGetParams {
params[k] = *v
}
Expand All @@ -40,9 +45,22 @@ var machinesGetCmd = &cobra.Command{
if err != nil {
return err
}
e := json.NewEncoder(cmd.OutOrStdout())
e.SetIndent("", " ")
return e.Encode(ms)
if ok && *outputFormat == "simple" {
ysksuzuki marked this conversation as resolved.
Show resolved Hide resolved
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 1, 1, ' ', 0)
w.Write([]byte("Serial\tRack\tRole\tState\tIPv4\tBMC\n"))
for _, m := range ms {
if len(m.Spec.IPv4) > 0 {
w.Write([]byte(fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t\n", m.Spec.Serial, m.Spec.Rack, m.Spec.Role, m.Status.State, m.Spec.IPv4[0], m.Spec.BMC.Type)))
} else {
w.Write([]byte(fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t\n", m.Spec.Serial, m.Spec.Rack, m.Spec.Role, m.Status.State, m.Spec.IPv6[0], m.Spec.BMC.Type)))
}
}
return w.Flush()
} else {
e := json.NewEncoder(cmd.OutOrStdout())
e.SetIndent("", " ")
return e.Encode(ms)
}
})
well.Stop()
return well.Wait()
Expand Down Expand Up @@ -191,19 +209,32 @@ var machinesSetRetireDateCmd = &cobra.Command{

func init() {
getOpts := map[string]string{
"serial": "Serial name",
"rack": "Rack name",
"role": "Role name",
"labels": "Label name and value (--labels key=val,...)",
"ipv4": "IPv4 address",
"ipv6": "IPv6 address",
"bmc-type": "BMC type",
"state": "State",
"serial": "Serial name(s) (--serial 001,002,003...)",
"rack": "Rack name(s) (--rack 1,2,3...)",
"role": "Role name(s) (--role boot,worker...)",
"labels": "Label name and value (--labels key=val,...)",
"ipv4": "IPv4 address(s) (--ipv4 10.0.0.1,10.0.0.2,10.0.0.3...)",
"ipv6": "IPv6 address(s) (--ipv6 aa::ff,bb::ff,cc::ff...)",
"bmc-type": "BMC type(s) (--bmc-type iDRAC-9,IPMI-2.0...)",
"state": "State(s) (--state retiring,uninitialized...)",
"without-serial": "without Serial name",
"without-rack": "without Rack name",
"without-role": "without Role name",
"without-labels": "without Label name and value (--labels key=val,...)",
"without-ipv4": "without IPv4 address",
"without-ipv6": "without IPv6 address",
"without-bmc-type": "without BMC type",
"without-state": "without State",
"output": "Output format",
}
for k, v := range getOpts {
val := new(string)
machinesGetParams[k] = val
machinesGetCmd.Flags().StringVar(val, k, "", v)
if k == "output" {
machinesGetCmd.Flags().StringVarP(val, k, "o", "", v)
} else {
machinesGetCmd.Flags().StringVar(val, k, "", v)
}
}
machinesCreateCmd.Flags().StringVarP(&machinesCreateFile, "file", "f", "", "machiens in json")
machinesCreateCmd.MarkFlagRequired("file")
Expand Down
237 changes: 216 additions & 21 deletions query.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package sabakan

import "fmt"
import "net/url"
import "strings"
import (
"fmt"
"net/url"
"strings"
)

// Query is an URL query
type Query map[string]string

// Match returns true if all non-empty fields matches Machine
func (q Query) Match(m *Machine) bool {
if serial := q["serial"]; len(serial) > 0 && serial != m.Spec.Serial {
return false
}
if ipv4 := q["ipv4"]; len(ipv4) > 0 {
if serial := q["serial"]; len(serial) > 0 {
match := false
for _, ip := range m.Spec.IPv4 {
if ip == ipv4 {
serials := strings.Split(serial, ",")
for _, s := range serials {
if s == m.Spec.Serial {
match = true
break
}
Expand All @@ -24,12 +24,30 @@ func (q Query) Match(m *Machine) bool {
return false
}
}
if ipv4 := q["ipv4"]; len(ipv4) > 0 {
ipv4s := strings.Split(ipv4, ",")
match := false
for _, ipv4address := range ipv4s {
for _, ip := range m.Spec.IPv4 {
if ip == ipv4address {
match = true
break
}
}
}
if !match {
return false
}
}
if ipv6 := q["ipv6"]; len(ipv6) > 0 {
ipv6s := strings.Split(ipv6, ",")
match := false
for _, ip := range m.Spec.IPv6 {
if ip == ipv6 {
match = true
break
for _, ipv6address := range ipv6s {
for _, ip := range m.Spec.IPv6 {
if ip == ipv6address {
match = true
break
}
}
}
if !match {
Expand All @@ -56,17 +74,147 @@ func (q Query) Match(m *Machine) bool {
}
}
}
if rack := q["rack"]; len(rack) > 0 && rack != fmt.Sprint(m.Spec.Rack) {
return false
if rack := q["rack"]; len(rack) > 0 {
racks := strings.Split(rack, ",")
match := false
for _, r := range racks {
if r == fmt.Sprint(m.Spec.Rack) {
match = true
break
}
}
if !match {
return false
}
}
if role := q["role"]; len(role) > 0 && role != m.Spec.Role {
return false
if role := q["role"]; len(role) > 0 {
roles := strings.Split(role, ",")
match := false
for _, r := range roles {
if r == m.Spec.Role {
match = true
break
}
}
if !match {
return false
}
}
if bmc := q["bmc-type"]; len(bmc) > 0 && bmc != m.Spec.BMC.Type {
return false
if bmc := q["bmc-type"]; len(bmc) > 0 {
bmcs := strings.Split(bmc, ",")
match := false
for _, b := range bmcs {
if b == m.Spec.BMC.Type {
match = true
break
}
}
if !match {
return false
}
}
if state := q["state"]; len(state) > 0 && state != m.Status.State.String() {
return false
if state := q["state"]; len(state) > 0 {
states := strings.Split(state, ",")
match := false
for _, s := range states {
if s == fmt.Sprint(m.Status.State) {
match = true
break
}
}
if !match {
return false
}
}
if withoutSerial := q["without-serial"]; len(withoutSerial) > 0 {
withoutSerials := strings.Split(withoutSerial, ",")
for _, wr := range withoutSerials {
if wr == fmt.Sprint(m.Spec.Serial) {
return false
}
}
}
if withoutIPv4 := q["without-ipv4"]; len(withoutIPv4) > 0 {
withoutIPv4s := strings.Split(withoutIPv4, ",")
match := false
for _, wIPv4 := range withoutIPv4s {
for _, ip := range m.Spec.IPv4 {
if ip == wIPv4 {
match = true
break
}
}
}
if match {
return false
}
}
if withoutIPv6 := q["without-ipv6"]; len(withoutIPv6) > 0 {
withoutIPv6s := strings.Split(withoutIPv6, ",")
match := false
for _, wIPv6 := range withoutIPv6s {
for _, ip := range m.Spec.IPv6 {
if ip == wIPv6 {
match = true
break
}
}
}
if match {
return false
}
}
if withoutLabels := q["without-labels"]; len(withoutLabels) > 0 {
// Split into each query
rawQueries := strings.Split(withoutLabels, ",")
for _, rawQuery := range rawQueries {
rawQuery = strings.TrimSpace(rawQuery)
query, err := url.ParseQuery(rawQuery)
if err != nil {
return false
}
for k, v := range query {
if label, exists := m.Spec.Labels[k]; exists {
if v[0] == label {
return false
}
} else {
return false
}
}
}
}
if withoutRack := q["without-rack"]; len(withoutRack) > 0 {
withoutRacks := strings.Split(withoutRack, ",")
for _, wr := range withoutRacks {
if wr == fmt.Sprint(m.Spec.Rack) {
return false
}
}
}
if withoutRole := q["without-role"]; len(withoutRole) > 0 {
withoutRoles := strings.Split(withoutRole, ",")
for _, wr := range withoutRoles {
if wr == fmt.Sprint(m.Spec.Role) {
return false
}
}
}
if withoutBmc := q["without-bmc-type"]; len(withoutBmc) > 0 {
withoutBmcs := strings.Split(withoutBmc, ",")
for _, wb := range withoutBmcs {
if wb == fmt.Sprint(m.Spec.BMC.Type) {
return false
}
}
}
if withoutState := q["without-state"]; len(withoutState) > 0 {
withoutStates := strings.Split(withoutState, ",")
for _, ws := range withoutStates {
if ws == fmt.Sprint(m.Status.State) {
return false
}
}
}

return true
Expand Down Expand Up @@ -111,3 +259,50 @@ func (q Query) IsEmpty() bool {
}
return true
}

// RemoveWithout returns query removed --without key
func (q Query) HasOnlyWithout() bool {
for k, v := range q {
if !strings.HasPrefix(k, "without") && len(v) > 0 {
return false
}
}
return true
}

// Valid returns true if query isn't conflicted
func (q Query) Valid() bool {
hasWithoutSerial := q["without-serial"]
if hasSerial := q["serial"]; len(hasSerial) > 0 && len(hasWithoutSerial) > 0 {
return false
ysksuzuki marked this conversation as resolved.
Show resolved Hide resolved
}
hasWithoutRack := q["without-rack"]
if hasRack := q["rack"]; len(hasRack) > 0 && len(hasWithoutRack) > 0 {
return false
}
hasWithoutRole := q["without-role"]
if hasRole := q["role"]; len(hasRole) > 0 && len(hasWithoutRole) > 0 {
return false
}
hasWithoutIPv4 := q["without-ipv4"]
if hasIPv4 := q["ipv4"]; len(hasIPv4) > 0 && len(hasWithoutIPv4) > 0 {
return false
}
hasWithoutIPv6 := q["without-ipv6"]
if hasIPv6 := q["ipv6"]; len(hasIPv6) > 0 && len(hasWithoutIPv6) > 0 {
return false
}
hasWithoutBMCType := q["without-bmc-type"]
if hasBMCType := q["bmc-type"]; len(hasBMCType) > 0 && len(hasWithoutBMCType) > 0 {
return false
}
hasWithoutState := q["without-state"]
if hasState := q["state"]; len(hasState) > 0 && len(hasWithoutState) > 0 {
return false
}
hasWithoutLabels := q["without-labels"]
if hasLabels := q["labels"]; len(hasLabels) > 0 && len(hasWithoutLabels) > 0 {
return false
}
return true
}
Loading