Skip to content

Instantly share code, notes, and snippets.

View guitarrapc's full-sized avatar
:octocat:

Ikiru Yoshizaki guitarrapc

:octocat:
View GitHub Profile
@guitarrapc
guitarrapc / DotnetUwrapOptimization.cs
Last active October 26, 2021 13:35
Unbox T with Unsafe.Unbox<T>(val) will remove allocation. https://twitter.com/badamczewski01/status/1452918677855145988
BenchmarkRunner.Run<BenchmarkUnbox>();
[BenchmarkDotNet.Attributes.MemoryDiagnoser]
public class BenchmarkUnbox
{
[Benchmark]
public void Slow()
{
A(0);
}
@guitarrapc
guitarrapc / CSharp10_LambdaImprovement.cs
Last active November 6, 2021 06:18
C# 10 Lambda not has Natural Typeing, return type and Lambda Aatribute. https://ufcpp.net/study/csharp/cheatsheet/ap_ver10/#lambda-improvement
// only work after .NET 6
var x = 0;
var f =
[A]
[return: B]
static int ([C] int x)
=> x;
f(1).Dump(); // 1. not 0.
class AAttribute : Attribute{ }
@guitarrapc
guitarrapc / NullableOptimization.cs
Last active October 24, 2021 08:48
Nullable optimization. use is to determine Nullable<T> to T. https://twitter.com/badamczewski01/status/1452188929747308544
BenchmarkRunner.Run<BenchmarkNullable>();
[BenchmarkDotNet.Attributes.MemoryDiagnoser]
public class BenchmarkNullable
{
[Benchmark]
public void Slow()
{
NullaleSlow(0);
}
@guitarrapc
guitarrapc / delete_route53_alias_records.sh
Last active October 22, 2021 04:03
Clean up All A record alias in target HostedZone.
#!/bin/bash -e
# obtains records
hostedZone=xxxxxxxx
records=$(aws route53 list-resource-record-sets --hosted-zone-id "$hostedZone" --query 'ResourceRecordSets[?Type==`A`].[Name,AliasTarget.HostedZoneId,AliasTarget.DNSName]' --output text)
head=$(cat <<EOS
{
"Comment": "DELETE record",
"Changes": [
string? x = null;
IsNullOrEmpty(x).Dump();
NullableLength(x).Dump();
PropLength(x).Dump();
TypeOrEmpty(x).Dump();
TypeAndEmpty(x).Dump();
bool IsNullOrEmpty(string? x) => string.IsNullOrEmpty(x);
bool NullableLength(string? x) => x?.Length == 0 || x is null;
bool PropLength(string? x) => x is not {Length: > 0};
@guitarrapc
guitarrapc / main.js
Created September 27, 2021 04:39
Simply detect IPAddress is in specified CIDRs. no dependencies to any packages. ES5.1 compatible.
var range = getIpRange("192.168.0.0/24");
console.log(`range: min ${range.min} max ${range.max}`); // {min: "192.168.0.0", max: "192.168.0.255"}
var ip1 = getIpRange("192.168.0.5");
console.log(`ip1: min ${ip1.min} max ${ip1.max}`); // {min: "192.168.0.0", max: "192.168.0.255"}
var ip2 = getIpRange("192.168.0.100");
console.log(`ip2: min ${ip2.min} max ${ip2.max}`); // {min: "192.168.0.0", max: "192.168.0.255"}
var ip3 = getIpRange("192.168.1.1");
console.log(`ip3: min ${ip3.min} max ${ip3.max}`); // {min: "192.168.0.0", max: "192.168.0.255"}
if (ip1.min > range.min && ip1.max < range.max) {
@guitarrapc
guitarrapc / Regoメモ.md
Last active August 26, 2021 01:22
Rego を使って Kubernetes YAMLのマニフェストをテストする

tl;dr;

  • Rego のポリシーは、それぞれに名前つけたほうがテスト書いていて指定したポリシーと明示できるのがいい。けど、いちいち名前指定するのが面倒という意見もありそう。
  • Rego のポリシー自体が正しいか試行錯誤することになるので、先にテスト書いたほうがいい。テストを書いてから、実際のYAMLに流すの流れが効率的。
  • Rego のテストは、引数禁止、テストが何をしても通らなくなる。ダメな例: test_foo[msg] { }。いい例: test_foo { }
  • Rego のポリシーと結果をサクッと試すにはPlayground が便利。

Rego基本情報

基本的なコンセプト、構文、関数はここを参照

@guitarrapc
guitarrapc / IsEven.cs
Last active August 19, 2021 06:44
Even should not use mod.
void Main()
{
var summary = BenchmarkRunner.Run<BenchmarkEven>();
}
[BenchmarkDotNet.Attributes.MemoryDiagnoser]
public class BenchmarkEven
{
[Benchmark]
[Arguments(42)]
@guitarrapc
guitarrapc / Bicep 設計.md
Last active November 28, 2024 07:24
Azure Bicep を IaC として使うことを考える

tl;dr;

  • Bicep を用いて、コードとリソースの一致を保証することは ARM Template の現在の性質ではできない。Microsoft は Deployment Stacks と呼ばれる仕組みで改善を検討している。
  • Bicep は、Complete modeを使うとコードとリソースが一致することがある程度保障できる。IaCとして使うなら Complete 一択。(それでもずれる)
  • Completeでは、最後に適用したBiepデプロイでリソースが決定されるので、Bicep スコープは狭く維持する。このため、ResourceGroup を target scopeにするのがいいだろう。Bicepを分ける = ResourceGroupをライフサイクルで分けることになる。

Bicep と ARM Template

bicep は、ARM Template の DSLでARM Templateに変換される。あくまでDSLなので、デプロイを含めた仕様、制約はARM Template に準じる

More recent resolution:
1. cd ~/../../etc (go to etc folder in WSL).
2. echo "[network]" | sudo tee wsl.conf (Create wsl.conf file and add the first line).
3. echo "generateResolvConf = false" | sudo tee -a wsl.conf (Append wsl.conf the next line).
4. wsl --terminate Debian (Terminate WSL in Windows cmd, in case is Ubuntu not Debian).
5. cd ~/../../etc (go to etc folder in WSL).
6. sudo rm -Rf resolv.conf (Delete the resolv.conf file).
7. In windows cmd, ps or terminal with the vpn connected do: Get-NetIPInterface or ipconfig /all for get the dns primary and
secondary.