Skip to content

Commit

Permalink
reactor: remove usage of ID field
Browse files Browse the repository at this point in the history
As of drbd-reactor 1.2.0, this field is deprecated and generates a
warning. We should no longer rely on it in linstor-gateway.

Instead, use either the filename ("linstor-gateway-iscsi-xyz.toml")
or the name of the first promoter resource ("xyz") as an identifier.
  • Loading branch information
chrboe committed Nov 6, 2023
1 parent 89b3c80 commit 878d53f
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 175 deletions.
11 changes: 5 additions & 6 deletions pkg/common/resource_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ func ClusterPrivateVolumeAgent(deployedVol client.Volume, resource string) *reac
}

func CheckIPCollision(config reactor.PromoterConfig, checkIP net.IP) error {
var rscCfg reactor.PromoterResourceConfig
for _, v := range config.Resources {
rscCfg = v
name, rscCfg := config.FirstResource()
if rscCfg == nil {
return fmt.Errorf("no resource found in config")
}

for _, entry := range rscCfg.Start {
switch agent := entry.(type) {
case *reactor.ResourceAgent:
Expand All @@ -68,12 +67,12 @@ func CheckIPCollision(config reactor.PromoterConfig, checkIP net.IP) error {
ip := net.ParseIP(agent.Attributes["ip"])
if ip == nil {
return fmt.Errorf("malformed IP address %s in agent %s of config %s",
agent.Attributes["ip"], agent.Name, config.ID)
agent.Attributes["ip"], agent.Name, name)
}
log.Debugf("checking IP %s", ip)
if ip.Equal(checkIP) {
return fmt.Errorf("IP address %s already in use by config %s",
ip.String(), config.ID)
ip.String(), name)
}
}
}
Expand Down
60 changes: 29 additions & 31 deletions pkg/iscsi/iscsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/google/go-cmp/cmp"
"path/filepath"
"sort"
"time"

Expand All @@ -17,7 +18,10 @@ import (
"github.com/LINBIT/linstor-gateway/pkg/reactor"
)

const IDFormat = "iscsi-%s"
const (
IDFormat = "iscsi-%s"
FilenameFormat = "linstor-gateway-iscsi-%s.toml"
)

type ISCSI struct {
cli *linstorcontrol.Linstor
Expand Down Expand Up @@ -96,17 +100,14 @@ func (i *ISCSI) Create(ctx context.Context, rsc *ResourceConfig) (*ResourceConfi
return nil, fmt.Errorf("validation failed: %w", err)
}

var cfg *reactor.PromoterConfig
var path string
cfgID := fmt.Sprintf(IDFormat, rsc.IQN.WWN())
configs, paths, err := reactor.ListConfigs(ctx, i.cli.Client)
if err != nil {
return nil, fmt.Errorf("failed to retrieve existing configs: %w", err)
}

for i := range configs {
c := configs[i]
p := paths[i]
for j := range configs {
c := configs[j]
p := paths[j]

for _, ip := range rsc.ServiceIPs {
if err := common.CheckIPCollision(c, ip.IP()); err != nil {
Expand All @@ -116,19 +117,14 @@ func (i *ISCSI) Create(ctx context.Context, rsc *ResourceConfig) (*ResourceConfi

// while looking for ip collisions, filter out any existing config with
// the same name as the one we are trying to create.
if c.ID == cfgID {
cfg = &c
path = p
}
}

if cfg != nil {
deployedCfg, err := i.getExistingDeployment(ctx, rsc, cfg, path)
if err != nil {
return nil, err
}
if deployedCfg != nil {
return deployedCfg, nil
if configName, _ := c.FirstResource(); configName == rsc.IQN.WWN() {
deployedCfg, err := i.getExistingDeployment(ctx, rsc, &c, p)
if err != nil {
return nil, err
}
if deployedCfg != nil {
return deployedCfg, nil
}
}
}

Expand All @@ -153,12 +149,12 @@ func (i *ISCSI) Create(ctx context.Context, rsc *ResourceConfig) (*ResourceConfi
}
}()

cfg, err = rsc.ToPromoter(deployment)
cfg, err := rsc.ToPromoter(deployment)
if err != nil {
return nil, fmt.Errorf("failed to convert resource to promoter configuration: %w", err)
}

err = reactor.EnsureConfig(ctx, i.cli.Client, cfg)
err = reactor.EnsureConfig(ctx, i.cli.Client, cfg, rsc.ID())
if err != nil {
return nil, fmt.Errorf("failed to register reactor config file: %w", err)
}
Expand All @@ -168,13 +164,14 @@ func (i *ISCSI) Create(ctx context.Context, rsc *ResourceConfig) (*ResourceConfi
return nil, fmt.Errorf("failed to start resources: %w", err)
}

path := reactor.ConfigPath(rsc.ID())
rsc.Status = linstorcontrol.StatusFromResources(path, resourceDefinition, resourceGroup, deployment)

return rsc, nil
}

func (i *ISCSI) Start(ctx context.Context, iqn Iqn) (*ResourceConfig, error) {
cfg, _, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
cfg, path, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
if err != nil {
return nil, fmt.Errorf("failed to find the resource configuration: %w", err)
}
Expand All @@ -183,7 +180,7 @@ func (i *ISCSI) Start(ctx context.Context, iqn Iqn) (*ResourceConfig, error) {
return nil, nil
}

err = reactor.AttachConfig(ctx, i.cli.Client, cfg)
err = reactor.AttachConfig(ctx, i.cli.Client, cfg, path)
if err != nil {
return nil, fmt.Errorf("failed to detach reactor configuration: %w", err)
}
Expand All @@ -200,7 +197,7 @@ func (i *ISCSI) Start(ctx context.Context, iqn Iqn) (*ResourceConfig, error) {
}

func (i *ISCSI) Stop(ctx context.Context, iqn Iqn) (*ResourceConfig, error) {
cfg, _, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
cfg, path, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
if err != nil {
return nil, fmt.Errorf("failed to find the resource configuration: %w", err)
}
Expand All @@ -209,7 +206,7 @@ func (i *ISCSI) Stop(ctx context.Context, iqn Iqn) (*ResourceConfig, error) {
return nil, nil
}

err = reactor.DetachConfig(ctx, i.cli.Client, cfg)
err = reactor.DetachConfig(ctx, i.cli.Client, cfg, path)
if err != nil {
return nil, fmt.Errorf("failed to detach reactor configuration: %w", err)
}
Expand All @@ -235,11 +232,12 @@ func (i *ISCSI) List(ctx context.Context) ([]*ResourceConfig, error) {
for j := range cfgs {
cfg := &cfgs[j]
path := paths[j]
filename := filepath.Base(path)

var rsc string
n, _ := fmt.Sscanf(cfg.ID, IDFormat, &rsc)
if n == 0 {
log.WithField("id", cfg.ID).Trace("not an iscsi resource config, skipping")
num, _ := fmt.Sscanf(filename, FilenameFormat, &rsc)
if num == 0 {
log.WithField("filename", filename).Trace("not an iSCSI resource config, skipping")
continue
}

Expand Down Expand Up @@ -344,7 +342,7 @@ func (i *ISCSI) AddVolume(ctx context.Context, iqn Iqn, volCfg *common.VolumeCon
return nil, fmt.Errorf("failed to convert resource to promoter configuration: %w", err)
}

err = reactor.EnsureConfig(ctx, i.cli.Client, cfg)
err = reactor.EnsureConfig(ctx, i.cli.Client, cfg, deployedCfg.ID())
if err != nil {
return nil, fmt.Errorf("failed to update config: %w", err)
}
Expand Down Expand Up @@ -397,7 +395,7 @@ func (i *ISCSI) DeleteVolume(ctx context.Context, iqn Iqn, lun int) (*ResourceCo
return nil, fmt.Errorf("failed to convert resource to promoter configuration: %w", err)
}

err = reactor.EnsureConfig(ctx, i.cli.Client, cfg)
err = reactor.EnsureConfig(ctx, i.cli.Client, cfg, rscCfg.ID())
if err != nil {
return nil, fmt.Errorf("failed to update config")
}
Expand Down
18 changes: 4 additions & 14 deletions pkg/iscsi/resource_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,17 @@ const minAgentEntries = 4 // portblock, service_ip, target, portunblock

func parsePromoterConfig(cfg *reactor.PromoterConfig) (*ResourceConfig, error) {
r := &ResourceConfig{}
var res string
n, err := fmt.Sscanf(cfg.ID, IDFormat, &res)
if n != 1 {
return nil, fmt.Errorf("failed to parse id into resource name: %w", err)
}

if len(cfg.Resources) != 1 {
return nil, errors.New(fmt.Sprintf("promoter config without exactly 1 resource (has %d)", len(cfg.Resources)))
}

var rscCfg reactor.PromoterResourceConfig
for _, v := range cfg.Resources {
rscCfg = v
_, rscCfg := cfg.FirstResource()
if rscCfg == nil {
return nil, fmt.Errorf("promoter config without resource")
}

if len(rscCfg.Start) < minAgentEntries {
return nil, errors.New(fmt.Sprintf("config has too few agent entries, expected at least %d, got %d",
minAgentEntries, len(rscCfg.Start)))
}

var err error
var numPortblocks, numPortunblocks int
for _, entry := range rscCfg.Start {
switch agent := entry.(type) {
Expand Down Expand Up @@ -390,7 +381,6 @@ func (r *ResourceConfig) ToPromoter(deployment []client.ResourceWithVolumes) (*r
}

return &reactor.PromoterConfig{
ID: r.ID(),
Resources: map[string]reactor.PromoterResourceConfig{
r.IQN.WWN(): {
Runner: "systemd",
Expand Down
14 changes: 0 additions & 14 deletions pkg/iscsi/resource_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func TestParsePromoterConfig(t *testing.T) {
{
name: "default",
cfg: &reactor.PromoterConfig{
ID: "iscsi-target1",
Resources: map[string]reactor.PromoterResourceConfig{
"target1": {
Start: []reactor.StartEntry{
Expand All @@ -45,17 +44,9 @@ func TestParsePromoterConfig(t *testing.T) {
ServiceIPs: []common.IpCidr{ipnet("1.1.1.1/16")},
},
},
{
name: "invalid id",
cfg: &reactor.PromoterConfig{
ID: "target1",
},
wantErr: true,
},
{
name: "too many resources",
cfg: &reactor.PromoterConfig{
ID: "iscsi-target1",
Resources: map[string]reactor.PromoterResourceConfig{
"target1": {},
"target2": {},
Expand All @@ -66,15 +57,13 @@ func TestParsePromoterConfig(t *testing.T) {
{
name: "too few agent entries",
cfg: &reactor.PromoterConfig{
ID: "iscsi-target1",
Resources: map[string]reactor.PromoterResourceConfig{"target1": {Start: []reactor.StartEntry{}}},
},
wantErr: true,
},
{
name: "missing portblock",
cfg: &reactor.PromoterConfig{
ID: "iscsi-target1",
Resources: map[string]reactor.PromoterResourceConfig{
"target1": {
Start: []reactor.StartEntry{
Expand All @@ -91,7 +80,6 @@ func TestParsePromoterConfig(t *testing.T) {
{
name: "missing ip",
cfg: &reactor.PromoterConfig{
ID: "iscsi-target1",
Resources: map[string]reactor.PromoterResourceConfig{
"target1": {
Start: []reactor.StartEntry{
Expand All @@ -108,7 +96,6 @@ func TestParsePromoterConfig(t *testing.T) {
{
name: "multiple ips",
cfg: &reactor.PromoterConfig{
ID: "iscsi-target1",
Resources: map[string]reactor.PromoterResourceConfig{
"target1": {
Start: []reactor.StartEntry{
Expand All @@ -135,7 +122,6 @@ func TestParsePromoterConfig(t *testing.T) {
{
name: "with specific implementation",
cfg: &reactor.PromoterConfig{
ID: "iscsi-target1",
Resources: map[string]reactor.PromoterResourceConfig{
"target1": {
Start: []reactor.StartEntry{
Expand Down
Loading

0 comments on commit 878d53f

Please sign in to comment.