Skip to content

Commit

Permalink
Using ->addToAttribute()
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Nov 2, 2020
1 parent 08aa6e6 commit c82d7bf
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 44 deletions.
12 changes: 4 additions & 8 deletions src/Instances/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ public function setImage(string $image, string $tag = 'latest')
*/
public function addPort(int $containerPort, string $protocol = 'TCP', string $name = null)
{
$ports = array_merge($this->getAttribute('ports', []), [
[
'name' => $name,
'protocol' => $protocol,
'containerPort' => $containerPort,
],
return $this->addToAttribute('ports', [
'name' => $name,
'protocol' => $protocol,
'containerPort' => $containerPort,
]);

return $this->setAttribute('ports', $ports);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Kinds/K8sHorizontalPodAutoscaler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ public function setResource(Scalable $resource)
*/
public function addMetric(ResourceMetric $metric)
{
$metrics = array_merge($this->getMetrics(), [
$metric->toArray(),
]);

return $this->setSpec('metrics', $metrics);
return $this->addToSpec('metrics', $metric->toArray());
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Kinds/K8sIngress.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ public function setRules(array $rules = [])
*/
public function addRule(array $rule)
{
$rules = $this->getRules();

$rules = array_merge($rules, [$rule]);

return $this->setSpec('rules', $rules);
return $this->addToSpec('rules', $rule);
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/Kinds/K8sPod.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,7 @@ public function getInitContainers(bool $asInstance = true): array
*/
public function addPulledSecret(string $name)
{
$imagePullSecrets = $this->getAttribute('imagePullSecrets', []);

$imagePullSecrets = array_merge($imagePullSecrets, [
['name' => $name],
]);

return $this->setAttribute('imagePullSecrets', $imagePullSecrets);
return $this->addToAttribute('imagePullSecrets', ['name' => $name]);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Kinds/K8sService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ public function setPorts(array $ports = [])
*/
public function addPort(array $port)
{
$ports = $this->getPorts();

$ports = array_merge($ports, [$port]);

return $this->setSpec('ports', $ports);
return $this->addToSpec('ports', $port);
}

/**
Expand Down
16 changes: 2 additions & 14 deletions src/Kinds/K8sServiceAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ public function addSecret($secret)
$secret = $secret->getName();
}

$secrets = $this->getAttribute('secrets', []);

$secrets = array_merge($secrets, [
['name' => $secret],
]);

return $this->setAttribute('secrets', $secrets);
return $this->addToAttribute('secrets', ['name' => $secret]);
}

/**
Expand All @@ -65,13 +59,7 @@ public function addSecrets(array $secrets)
*/
public function addPulledSecret(string $name)
{
$imagePullSecrets = $this->getAttribute('imagePullSecrets', []);

$imagePullSecrets = array_merge($imagePullSecrets, [
['name' => $name],
]);

return $this->setAttribute('imagePullSecrets', $imagePullSecrets);
return $this->addToAttribute('imagePullSecrets', ['name' => $name]);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ public function setAttribute(string $name, $value)
return $this;
}

/**
* For an array attribute, append a new element to the list.
*
* @param string $name
* @param mixed $value
* @return $this
*/
public function addToAttribute(string $name, $value)
{
$current = $this->getAttribute($name, []);

if (! is_array($current)) {
return $this;
}

return $this->setAttribute($name, array_merge($current, [$value]));
}

/**
* Remove an attribute.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Traits/HasSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public function setSpec(string $name, $value)
return $this->setAttribute("spec.{$name}", $value);
}

/**
* Append a value to the spec parameter, if array.
*
* @param string $name
* @param mixed $value
* @return $this
*/
public function addToSpec(string $name, $value)
{
return $this->addToAttribute("spec.{$name}", $value);
}

/**
* Get the spec parameter with default.
*
Expand Down

0 comments on commit c82d7bf

Please sign in to comment.